public void initialize() { Controller ctrlr = this; JMenuItem mi = createMenuItem(ctrlr, "DTD to Schema", new Integer('D')); // NOTRANS Ide.getMenubar() .add( mi, MenuManager.getJMenu(IdeMainWindow.MENU_TOOLS), MenuConstants.SECTION_TOOLS_ADDINS); }
/** * The controller implementation is responsible for enabling and disabling actions and performing * any work required when they are invoked. */ @RegisteredByExtension("net.sourceforge.pmd.jdeveloper") public final class PmdController implements Controller { /** * Look up the numeric id of the action we defined in our extension manifest. The string constant * matches the value of the id attribute in the action element of the extension manifest. * * <p>The numeric id can be used to retrieve an instance of IdeAction. */ public static final int RUN_PMD_CMD_ID = Ide.findCmdID("net.sourceforge.pmd.jdeveloper.CheckPmd"); public boolean handleEvent(final IdeAction action, final Context context) { // Command is handled by net.sourceforge.pmd.jdeveloper.PmdCommand return false; } public boolean update(final IdeAction action, final Context context) { final Element doc = context.getElement(); final View view = context.getView(); // RelativeDirectoryContextFolder -> a package if (doc instanceof Project || doc instanceof JavaSourceNode || doc instanceof RelativeDirectoryContextFolder || view instanceof CodeEditor) { action.setEnabled(true); // TODO CPD // contextMenu.add(contextMenu.createMenuItem(cpdAction)); } else { action.setEnabled(false); } return true; } }
private boolean oldPersistenceMappingFileExists() { Project appControllerProject = McAppUtils.getApplicationControllerProject(Ide.getActiveWorkspace(), null); String fileName = "persistenceMapping.xml"; URL sourceURL = FileUtils.getSourceURL(appControllerProject, "META-INF", fileName); String content = FileUtils.getStringFromInputStream(FileUtils.getInputStream(sourceURL)); return content != null; }
private static JMenuItem createMenuItem(Controller ctrlr, String menuLabel, Integer mnemonic) { eiAction = IdeAction.get( DTD_TO_SCHEMA_CMD_ID, // int cmdId, null, menuLabel, // String name, null, mnemonic, // Integer mnemonic, null, null, true); eiAction.addController(ctrlr); JMenuItem menuItem = Ide.getMenubar().createMenuItem(eiAction); return menuItem; }
public boolean handleEvent(IdeAction action, Context context) { int cmdId = action.getCommandId(); String msg = ""; boolean ret = false; if (cmdId == DTD_TO_SCHEMA_CMD_ID) { TextNode element = null; if (context.getElement() != null && context.getElement() instanceof TextNode && context.getElement().getShortLabel().toUpperCase().endsWith(".DTD")) element = (TextNode) context.getElement(); if (element == null) // Check Editor { Editor editor = EditorManager.getEditorManager().getCurrentEditor(); if (editor != null) { if (editor.getContext().getNode() instanceof TextNode && editor.getContext().getNode().getShortLabel().toUpperCase().endsWith(".DTD")) { element = (TextNode) editor.getContext().getNode(); } } } if (element != null) { try { final TextNode dtdNode = (TextNode) element; // We need: // The root name // The path to the schema to generate final Project currentPrj = Ide.getActiveProject(); final String srcPath = JProjectUtil.getProjectBasePath(currentPrj).getFile().toString(); if (dp == null) dp = new DialogPanel(); dp.setDefaultLocation(srcPath); // Set the DTD root list URL dtdURL = dtdNode.getURL(); // get the DTD object dp.getRootCombo().removeAllItems(); DTD dtdNoRoot = getDTD(dtdURL, null); NamedNodeMap map = dtdNoRoot.getElementDecls(); for (int i = 0; i < map.getLength(); i++) { Node node = map.item(i); dp.getRootCombo().addItem(node.getNodeName()); } // Suggest a default name for the schema String dtdFullName = dtdNode.getLongLabel(); String xsdFullName = dtdFullName.substring(0, dtdFullName.lastIndexOf(".dtd")) + ".xsd"; dp.getSchemaNameFld().setText(xsdFullName); JEWTDialog dlg = OnePageWizardDialogFactory.createJEWTDialog(dp, null, "DTD to Schema"); dlg.addVetoableChangeListener(new LocalVetoableChangeListener()); dlg.setDefaultButton(JEWTDialog.BUTTON_OK); dlg.setOKButtonEnabled(true); String topic = "dtd_to_schema_html"; HelpSystem.getHelpSystem().registerTopic((JComponent) dlg.getContent(), topic); boolean go = WizardLauncher.runDialog(dlg); if (go) { final String root = dp.getRoot(); final String fName = dp.getSchemaName(); class SchemaGenerationRunnable implements Runnable { XmlSchemaNode xsn = null; boolean ok = true; public void run() { try { URL dtdURL = dtdNode.getURL(); // get the DTD object DTD dtd = getDTD(dtdURL, root); // convert DTD to Schema DOM tree XMLDocument dtddoc = dtd.convertDTD2Schema(); URL classURL = URLFactory.newURL(fName); if (classURL == null) { JOptionPane.showMessageDialog( null, "Cannot create URL for " + fName, "Ooops", JOptionPane.ERROR_MESSAGE); // ok2go.setValue(false); ok = false; } else { // Let's go on xsn = (XmlSchemaNode) NodeFactory.findOrCreate(XmlSchemaNode.class, classURL); currentPrj.add(xsn, true); xsn.open(); // Now the Node is created, let's create its content TextBuffer tb = xsn.acquireTextBuffer(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintWriter pw = new PrintWriter(baos); dtddoc.print(pw); String content = baos.toString(); tb.beginEdit(); tb.append(content.toCharArray()); tb.endEdit(); ok = true; } } catch (Exception e) { e.printStackTrace(); } } public boolean isOk() { return ok; } public XmlSchemaNode getXmlSchemaNode() { return xsn; } } SchemaGenerationRunnable schemaGeneration = new SchemaGenerationRunnable(); SchemaGenerationProgress generationProgress = new SchemaGenerationProgress(schemaGeneration); final String LABEL = "Converting..."; ProgressBar progress = new ProgressBar(Ide.getMainWindow(), LABEL, generationProgress, true); generationProgress.setProgressBar(progress); progress.start("Please wait", ""); // reset generationProgress = null; progress = null; if (schemaGeneration.isOk()) { // Open the created node EditorManager.getEditorManager() .openDefaultEditorInFrame(schemaGeneration.getXmlSchemaNode().getURL()); // Add the compilation date SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:dd a"); // NOTRANS msg += ("Schema generation [" + sdf.format(new Date()) + "] " + " successful."); // Display message in log window. logMessage(msg); msg = ""; // reset } } ret = true; } catch (Exception ex) { logMessage("Error:" + ex.toString()); ex.printStackTrace(); } } else logMessage("element is null"); } if (msg != null && msg.trim().length() > 0) { logMessage(msg); // Ide.getStatusBar().setText(msg); ret = true; } return ret; }
public final class DTD2Schema implements Addin, Controller { public static final int DTD_TO_SCHEMA_CMD_ID = Ide.findOrCreateCmdID("xdk.DTD_TO_SCHEMA_CMD_ID"); // NOTRANS private DialogPanel dp = null; public boolean handleEvent(IdeAction action, Context context) { int cmdId = action.getCommandId(); String msg = ""; boolean ret = false; if (cmdId == DTD_TO_SCHEMA_CMD_ID) { TextNode element = null; if (context.getElement() != null && context.getElement() instanceof TextNode && context.getElement().getShortLabel().toUpperCase().endsWith(".DTD")) element = (TextNode) context.getElement(); if (element == null) // Check Editor { Editor editor = EditorManager.getEditorManager().getCurrentEditor(); if (editor != null) { if (editor.getContext().getNode() instanceof TextNode && editor.getContext().getNode().getShortLabel().toUpperCase().endsWith(".DTD")) { element = (TextNode) editor.getContext().getNode(); } } } if (element != null) { try { final TextNode dtdNode = (TextNode) element; // We need: // The root name // The path to the schema to generate final Project currentPrj = Ide.getActiveProject(); final String srcPath = JProjectUtil.getProjectBasePath(currentPrj).getFile().toString(); if (dp == null) dp = new DialogPanel(); dp.setDefaultLocation(srcPath); // Set the DTD root list URL dtdURL = dtdNode.getURL(); // get the DTD object dp.getRootCombo().removeAllItems(); DTD dtdNoRoot = getDTD(dtdURL, null); NamedNodeMap map = dtdNoRoot.getElementDecls(); for (int i = 0; i < map.getLength(); i++) { Node node = map.item(i); dp.getRootCombo().addItem(node.getNodeName()); } // Suggest a default name for the schema String dtdFullName = dtdNode.getLongLabel(); String xsdFullName = dtdFullName.substring(0, dtdFullName.lastIndexOf(".dtd")) + ".xsd"; dp.getSchemaNameFld().setText(xsdFullName); JEWTDialog dlg = OnePageWizardDialogFactory.createJEWTDialog(dp, null, "DTD to Schema"); dlg.addVetoableChangeListener(new LocalVetoableChangeListener()); dlg.setDefaultButton(JEWTDialog.BUTTON_OK); dlg.setOKButtonEnabled(true); String topic = "dtd_to_schema_html"; HelpSystem.getHelpSystem().registerTopic((JComponent) dlg.getContent(), topic); boolean go = WizardLauncher.runDialog(dlg); if (go) { final String root = dp.getRoot(); final String fName = dp.getSchemaName(); class SchemaGenerationRunnable implements Runnable { XmlSchemaNode xsn = null; boolean ok = true; public void run() { try { URL dtdURL = dtdNode.getURL(); // get the DTD object DTD dtd = getDTD(dtdURL, root); // convert DTD to Schema DOM tree XMLDocument dtddoc = dtd.convertDTD2Schema(); URL classURL = URLFactory.newURL(fName); if (classURL == null) { JOptionPane.showMessageDialog( null, "Cannot create URL for " + fName, "Ooops", JOptionPane.ERROR_MESSAGE); // ok2go.setValue(false); ok = false; } else { // Let's go on xsn = (XmlSchemaNode) NodeFactory.findOrCreate(XmlSchemaNode.class, classURL); currentPrj.add(xsn, true); xsn.open(); // Now the Node is created, let's create its content TextBuffer tb = xsn.acquireTextBuffer(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintWriter pw = new PrintWriter(baos); dtddoc.print(pw); String content = baos.toString(); tb.beginEdit(); tb.append(content.toCharArray()); tb.endEdit(); ok = true; } } catch (Exception e) { e.printStackTrace(); } } public boolean isOk() { return ok; } public XmlSchemaNode getXmlSchemaNode() { return xsn; } } SchemaGenerationRunnable schemaGeneration = new SchemaGenerationRunnable(); SchemaGenerationProgress generationProgress = new SchemaGenerationProgress(schemaGeneration); final String LABEL = "Converting..."; ProgressBar progress = new ProgressBar(Ide.getMainWindow(), LABEL, generationProgress, true); generationProgress.setProgressBar(progress); progress.start("Please wait", ""); // reset generationProgress = null; progress = null; if (schemaGeneration.isOk()) { // Open the created node EditorManager.getEditorManager() .openDefaultEditorInFrame(schemaGeneration.getXmlSchemaNode().getURL()); // Add the compilation date SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:dd a"); // NOTRANS msg += ("Schema generation [" + sdf.format(new Date()) + "] " + " successful."); // Display message in log window. logMessage(msg); msg = ""; // reset } } ret = true; } catch (Exception ex) { logMessage("Error:" + ex.toString()); ex.printStackTrace(); } } else logMessage("element is null"); } if (msg != null && msg.trim().length() > 0) { logMessage(msg); // Ide.getStatusBar().setText(msg); ret = true; } return ret; } private DTD getDTD(URL dtdURL, String rootName) throws Exception { DOMParser parser = new DOMParser(); DTD dtd; parser.setValidationMode(DOMParser.WARNING); parser.setErrorStream(System.out); parser.showWarnings(true); parser.parseDTD(dtdURL, rootName); dtd = (DTD) parser.getDoctype(); return dtd; } class LocalVetoableChangeListener implements VetoableChangeListener { public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException { Object source = evt.getSource(); if (source instanceof JEWTDialog) { JEWTDialog dialog = (JEWTDialog) source; if (JEWTDialog.isDialogClosingEvent(evt)) { DialogPanel dp = (DialogPanel) dialog.getContent(); if (dp.getRoot().trim().length() == 0) { // Display Alert and request focus JOptionPane.showMessageDialog( dialog, "Root cannot be blank", "DTD to Schema", JOptionPane.ERROR_MESSAGE); dp.getRootCombo().requestFocus(); throw new PropertyVetoException("Root cannot be blank", evt); } if (dp.getSchemaName().trim().length() == 0) { // Display Alert and request focus JOptionPane.showMessageDialog( dialog, "Schema name cannot be blank", "DTD to Schema", JOptionPane.ERROR_MESSAGE); dp.getSchemaNameFld().requestFocus(); throw new PropertyVetoException("Schema name cannot be blank", evt); } } } else System.out.println("No action taken"); // NOTRANS } } public static String superTrim(String s) { String str = s.replace('\n', ' '); // NOTRANS while (str.startsWith(" ")) // NOTRANS str = str.substring(1); return str.trim(); } public static String rework(String fName) { String str = fName; if (str.startsWith("file:/")) // NOTRANS str = str.substring("file:/".length()); // NOTRANS str = str.replace('/', File.separatorChar); return str; } private static final void logMessage(String msg) { LogManager.getLogManager().showLog(); LogManager.getLogManager().getMsgPage().log(msg + "\n"); // NOTRANS } static boolean enableDTD2Schema(Context context) { Element element = (context != null ? context.getElement() : null); boolean bEnable = ((element != null)); if (bEnable) { if (element instanceof TextNode && element.getShortLabel().toUpperCase().endsWith(".DTD")) bEnable = true; else bEnable = false; } // In case the Navigator returns nothing, check the Editor if (!bEnable) { Editor editor = EditorManager.getEditorManager().getCurrentEditor(); if (editor != null) { if (editor.getContext().getNode() instanceof TextNode && editor.getContext().getNode().getShortLabel().toUpperCase().endsWith(".DTD")) { bEnable = true; } } } return bEnable; } public boolean update(IdeAction action, Context context) { int cmdId = action.getCommandId(); if (cmdId == DTD_TO_SCHEMA_CMD_ID) { action.setEnabled(enableDTD2Schema(context)); return true; } return false; } protected static IdeAction runAction; protected static IdeAction eiAction; public void initialize() { Controller ctrlr = this; JMenuItem mi = createMenuItem(ctrlr, "DTD to Schema", new Integer('D')); // NOTRANS Ide.getMenubar() .add( mi, MenuManager.getJMenu(IdeMainWindow.MENU_TOOLS), MenuConstants.SECTION_TOOLS_ADDINS); } private static JMenuItem createMenuItem(Controller ctrlr, String menuLabel, Integer mnemonic) { eiAction = IdeAction.get( DTD_TO_SCHEMA_CMD_ID, // int cmdId, null, menuLabel, // String name, null, mnemonic, // Integer mnemonic, null, null, true); eiAction.addController(ctrlr); JMenuItem menuItem = Ide.getMenubar().createMenuItem(eiAction); return menuItem; } }
public String getWorkspaceName() { String name = Ide.getActiveWorkspace().getShortLabel(); // strip off ".jws" return name.substring(0, (name.length() - 4)); }