1 19 20 21 package org.netbeans.modules.i18n; 22 23 24 import java.awt.Dialog ; 25 import java.io.IOException ; 26 import javax.swing.JEditorPane ; 27 import javax.swing.JPanel ; 28 import javax.swing.text.BadLocationException ; 29 import javax.swing.text.Position ; 30 import org.openide.DialogDescriptor; 31 import org.openide.ErrorManager; 32 33 import org.openide.cookies.EditorCookie; 34 import org.openide.nodes.Node; 35 import org.openide.NotifyDescriptor; 36 import org.openide.DialogDisplayer; 37 import org.openide.loaders.DataObject; 38 import org.openide.text.NbDocument; 39 import org.openide.util.HelpCtx; 40 import org.openide.util.actions.CookieAction; 41 import org.netbeans.api.project.FileOwnerQuery; 42 43 44 51 public class InsertI18nStringAction extends CookieAction { 52 53 54 static final long serialVersionUID =-7002111874047983222L; 55 56 58 private transient Position position; 60 61 private transient I18nSupport support; 62 63 private transient I18nPanel i18nPanel; 64 65 private transient DataObject dataObject; 66 67 public InsertI18nStringAction() { 68 putValue("noIconInMenu", Boolean.TRUE); 69 } 70 71 75 protected void performAction (final Node[] activatedNodes) { 76 try { 77 final EditorCookie editorCookie = (EditorCookie)(activatedNodes[0]).getCookie(EditorCookie.class); 78 if (editorCookie == null) { 79 Util.debug(new IllegalArgumentException ("Missing editor cookie!")); return; 81 } 82 83 dataObject = (DataObject)activatedNodes[0].getCookie(DataObject.class); 85 if (dataObject == null) { 86 Util.debug(new IllegalArgumentException ("Missing DataObject!")); return; 88 } 89 90 JEditorPane [] panes = editorCookie.getOpenedPanes(); 91 92 if (panes == null || panes.length == 0) { 93 Util.debug(new IllegalArgumentException ("Missing editor pane!")); return; 98 } 99 100 position = NbDocument.createPosition(panes[0].getDocument(), panes[0].getCaret().getDot(), Position.Bias.Backward); 102 103 I18nManager.getDefault().cancel(); 105 106 try { 107 showModalPanel(); 108 } catch(IOException ex) { 109 String msg = "Document loading failure " + dataObject.getName(); Util.debug(msg, ex); 111 return; 112 } 113 114 panes[0].getCaret().setVisible(true); 116 } catch (BadLocationException blex) { 117 ErrorManager.getDefault().notify(blex); 118 } finally { 119 dataObject = null; 120 support = null; 121 i18nPanel = null; 122 position = null; 123 } 124 } 125 126 127 130 private void insertI18nString() { 131 try { 132 I18nString i18nString = i18nPanel.getI18nString(); 133 134 if(i18nString.key == null) { 135 return; 136 } 137 138 support.getResourceHolder().addProperty( 140 i18nString.getKey(), 141 i18nString.getValue(), 142 i18nString.getComment() 143 ); 144 145 if(support.hasAdditionalCustomizer()) 148 support.performAdditionalChanges(); 149 150 String code = i18nString.getReplaceString(); 152 support.getDocument().insertString(position.getOffset(), code, null); 153 154 } catch (IllegalStateException e) { 155 NotifyDescriptor.Message msg = new NotifyDescriptor.Message( 156 I18nUtil.getBundle().getString("EXC_BadKey"), 157 NotifyDescriptor.ERROR_MESSAGE); 158 DialogDisplayer.getDefault().notify(msg); 159 } catch (BadLocationException e) { 160 DialogDisplayer.getDefault().notify( 161 new NotifyDescriptor.Message( 162 I18nUtil.getBundle().getString("MSG_CantInsertInGuarded"), 163 NotifyDescriptor.INFORMATION_MESSAGE 164 ) 165 ); 166 } 167 } 168 169 170 173 private JPanel createPanel() throws IOException { 174 I18nSupport.Factory factory = FactoryRegistry.getFactory(dataObject.getClass()); 175 176 if(factory == null) 177 throw new IllegalStateException ("I18N: No factory registered for data object type="+dataObject.getClass().getName()); 179 support = factory.create(dataObject); 180 181 184 i18nPanel = new I18nPanel(support.getPropertyPanel(), false, Util.getProjectFor(dataObject), dataObject.getPrimaryFile()); 185 i18nPanel.setI18nString(support.getDefaultI18nString()); 186 i18nPanel.setDefaultResource(dataObject); 187 188 return i18nPanel; 189 } 190 191 192 196 private void showModalPanel() throws IOException { 197 DialogDescriptor dd = new DialogDescriptor( 198 createPanel(), 199 Util.getString("CTL_InsertI18nDialogTitle"), 200 true, 201 NotifyDescriptor.OK_CANCEL_OPTION, 202 NotifyDescriptor.OK_OPTION, 203 DialogDescriptor.DEFAULT_ALIGN, 204 new HelpCtx(InsertI18nStringAction.class), 205 null 206 ); 207 Dialog dialog = DialogDisplayer.getDefault().createDialog(dd); 208 dialog.setVisible(true); 209 if (dd.getValue() == NotifyDescriptor.OK_OPTION) { 210 insertI18nString(); 211 } 212 } 213 214 215 217 protected boolean enable(Node[] activatedNodes) { 218 if (!super.enable(activatedNodes)) return false; 219 220 236 238 DataObject dataObject = (DataObject)activatedNodes[0].getCookie(DataObject.class); 239 240 if (dataObject == null) return false; 241 242 if (FileOwnerQuery.getOwner(dataObject.getPrimaryFile()) == null) return false; 244 245 if (FactoryRegistry.hasFactory(dataObject.getClass()) == false) return false; 246 247 EditorCookie sec = (EditorCookie)(activatedNodes[0]).getCookie(EditorCookie.class); 248 if (sec == null) return false; 249 250 JEditorPane [] edits = sec.getOpenedPanes(); 251 return edits != null && edits.length > 0; 252 } 253 254 257 protected int mode () { 258 return MODE_EXACTLY_ONE; 259 } 260 261 264 protected Class [] cookieClasses () { 265 return new Class [] { 266 EditorCookie.class, 267 }; 268 } 269 270 271 public String getName() { 272 return I18nUtil.getBundle().getString("CTL_InsertI18nString"); 273 } 274 275 276 public HelpCtx getHelpCtx() { 277 return new HelpCtx(I18nUtil.HELP_ID_MANINSERT); 278 } 279 280 protected boolean asynchronous() { 281 return false; 282 } 283 } 284 | Popular Tags |