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; }
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 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; }
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; }