Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
*/ |
40 |
package org.netbeans.modules.php.twig.editor.palette; |
41 |
|
42 |
import java.awt.event.ActionEvent; |
43 |
import java.io.IOException; |
44 |
import java.util.logging.Level; |
45 |
import java.util.logging.Logger; |
46 |
import javax.swing.AbstractAction; |
47 |
import javax.swing.Action; |
48 |
import javax.swing.text.JTextComponent; |
49 |
import org.netbeans.api.editor.mimelookup.MimeRegistration; |
50 |
import org.netbeans.editor.Utilities; |
51 |
import org.netbeans.modules.php.twig.editor.gsf.TwigLanguage; |
52 |
import org.netbeans.spi.palette.DragAndDropHandler; |
53 |
import org.netbeans.spi.palette.PaletteActions; |
54 |
import org.netbeans.spi.palette.PaletteController; |
55 |
import org.netbeans.spi.palette.PaletteFactory; |
56 |
import org.openide.*; |
57 |
import org.openide.text.ActiveEditorDrop; |
58 |
import org.openide.util.Lookup; |
59 |
import org.openide.util.NbBundle; |
60 |
import org.openide.util.datatransfer.ExTransferable; |
61 |
|
62 |
public class TwigPaletteFactory { |
63 |
|
64 |
private static final String TWIG_PALETTE_FOLDER = "Palettes/Twig"; // NOI18N |
65 |
private static PaletteController controller = null; |
66 |
|
67 |
@MimeRegistration(mimeType = TwigLanguage.TWIG_MIME_TYPE, service = PaletteController.class) |
68 |
public static PaletteController createPalette() throws IOException { |
69 |
if (controller == null) { |
70 |
controller = PaletteFactory.createPalette( |
71 |
TWIG_PALETTE_FOLDER, |
72 |
new TwigPaletteActions(), |
73 |
null, |
74 |
new TwigPaletteDragAndDropHandler() |
75 |
); |
76 |
} |
77 |
return controller; |
78 |
} |
79 |
|
80 |
//~ Inner classes |
81 |
private static class TwigPaletteActions extends PaletteActions { |
82 |
|
83 |
@Override |
84 |
public Action[] getImportActions() { |
85 |
return new Action[0]; |
86 |
} |
87 |
|
88 |
@Override |
89 |
public Action[] getCustomPaletteActions() { |
90 |
return new Action[0]; |
91 |
} |
92 |
|
93 |
@Override |
94 |
public Action[] getCustomCategoryActions(Lookup category) { |
95 |
return new Action[0]; |
96 |
} |
97 |
|
98 |
@Override |
99 |
public Action[] getCustomItemActions(Lookup item) { |
100 |
return new Action[0]; |
101 |
} |
102 |
|
103 |
@Override |
104 |
public Action getPreferredAction(Lookup item) { |
105 |
return new TwigPaletteInsertAction(item); |
106 |
} |
107 |
|
108 |
} |
109 |
|
110 |
private static class TwigPaletteInsertAction extends AbstractAction { |
111 |
|
112 |
private static final long serialVersionUID = -2545383594436146990L; |
113 |
|
114 |
private final Lookup item; |
115 |
|
116 |
TwigPaletteInsertAction(Lookup item) { |
117 |
this.item = item; |
118 |
} |
119 |
|
120 |
@NbBundle.Messages("TwigPaletteInsertAction.ErrorNoFocusedDocument=No document selected. Please select a document to insert the item into.") |
121 |
@Override |
122 |
public void actionPerformed(ActionEvent event) { |
123 |
|
124 |
ActiveEditorDrop drop = item.lookup(ActiveEditorDrop.class); |
125 |
|
126 |
JTextComponent target = Utilities.getFocusedComponent(); |
127 |
if (target == null) { |
128 |
String msg = Bundle.TwigPaletteInsertAction_ErrorNoFocusedDocument(); |
129 |
DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(msg, NotifyDescriptor.ERROR_MESSAGE)); |
130 |
return; |
131 |
} |
132 |
|
133 |
if (drop == null) { |
134 |
Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.INFO, "{0} doesn''t provide {1}", new Object[]{item.getClass(), ActiveEditorDrop.class}); //NOI18N |
135 |
return; |
136 |
} |
137 |
|
138 |
try { |
139 |
drop.handleTransfer(target); |
140 |
} finally { |
141 |
Utilities.requestFocus(target); |
142 |
} |
143 |
|
144 |
try { |
145 |
PaletteController paletteController = TwigPaletteFactory.createPalette(); |
146 |
paletteController.clearSelection(); |
147 |
} catch (IOException ioe) { |
148 |
Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.INFO, null, ioe); |
149 |
} |
150 |
|
151 |
} |
152 |
} |
153 |
|
154 |
private static class TwigPaletteDragAndDropHandler extends DragAndDropHandler { |
155 |
|
156 |
public TwigPaletteDragAndDropHandler() { |
157 |
super(true); |
158 |
} |
159 |
|
160 |
@Override |
161 |
public void customize(ExTransferable t, Lookup item) { |
162 |
} |
163 |
|
164 |
} |
165 |
} |