Esempio n. 1
0
 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
 }
Esempio n. 2
0
  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;
  }