// ------------------------------------------------------------------
  // from Java Swing 1.2 Orielly - Robert Eckstein
  // ------------------------------------------------------------------
  protected JTextComponent updateKeymapForWord(JTextComponent textComp) {
    // create a new child keymap
    Keymap map = JTextComponent.addKeymap("NslmMap", textComp.getKeymap());

    // define the keystrokeds to be added
    KeyStroke next = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, InputEvent.CTRL_MASK, false);
    // add the new mappings used DefaultEditorKit actions
    map.addActionForKeyStroke(next, getAction(DefaultEditorKit.nextWordAction));

    KeyStroke prev = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, InputEvent.CTRL_MASK, false);
    map.addActionForKeyStroke(prev, getAction(DefaultEditorKit.previousWordAction));

    KeyStroke selNext =
        KeyStroke.getKeyStroke(
            KeyEvent.VK_RIGHT, InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK, false);
    map.addActionForKeyStroke(selNext, getAction(DefaultEditorKit.selectionNextWordAction));
    KeyStroke selPrev =
        KeyStroke.getKeyStroke(
            KeyEvent.VK_LEFT, InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK, false);
    map.addActionForKeyStroke(selPrev, getAction(DefaultEditorKit.selectionPreviousWordAction));

    KeyStroke find = KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_MASK, false);
    map.addActionForKeyStroke(find, getAction("find"));

    KeyStroke findAgain = KeyStroke.getKeyStroke(KeyEvent.VK_G, InputEvent.CTRL_MASK, false);
    map.addActionForKeyStroke(findAgain, getAction("findAgain"));

    // set the keymap for the text component
    textComp.setKeymap(map);
    return (textComp);
  } // end updateKeymapForWord
 /**
  * References checked: 8.10.2003 (2.0b2)
  *
  * @param editorType editor type, one of this constants
  *     <UL>
  *       <LI>EDITOR_TYPE_DEFAULT
  *       <LI>EDITOR_TYPE_STYLED
  *       <LI>EDITOR_TYPE_SINGLE_LINE
  *     </UL>
  *
  * @param controler if not <code>null</code> for <code>EDITOR_TYPE_STYLED</code> editors a toolbar
  *     is created
  * @see PresentationDetail#PresentationDetail
  */
 TextEditorPanel(
     int editorType,
     GraphPanelControler controler,
     boolean showToolbar,
     PresentationDetail detail) {
   this.editorType = editorType;
   this.controler = controler;
   this.showToolbar = showToolbar;
   this.detail = detail;
   setLayout(new BorderLayout());
   // --- build this text editor panel ---
   switch (editorType) {
     case EDITOR_TYPE_DEFAULT:
       textComponent = new JTextArea();
       add(new JScrollPane(textComponent));
       break;
     case EDITOR_TYPE_STYLED:
       textComponent = new JTextPane();
       textComponent.setTransferHandler(new TextTransferHandler()); // ### requires Java 1.4
       // --- add toolbar ---
       if (showToolbar) {
         JPanel bar = new JPanel();
         Hashtable actions = DeepaMehtaClientUtils.createActionTable(textComponent);
         bar.setBackground(COLOR_PROPERTY_PANEL);
         toolbarButtons = new JButton[3];
         addButton(bar, new StyledEditorKit.BoldAction(), actions, controler.boldIcon());
         addButton(bar, new StyledEditorKit.ItalicAction(), actions, controler.italicIcon());
         addButton(bar, new StyledEditorKit.UnderlineAction(), actions, controler.underlineIcon());
         // ### addButton(bar, "H1", CMD_SET_HEADLINE1);
         // ### addButton(bar, "H2", CMD_SET_HEADLINE2);
         add(bar, BorderLayout.SOUTH);
       }
       add(new JScrollPane(textComponent));
       break;
     case EDITOR_TYPE_SINGLE_LINE:
       textComponent = new JTextField();
       ((JTextField) textComponent).addActionListener(this);
       add(textComponent);
       break;
     default:
       throw new DeepaMehtaException("unexpected text editor type: " + editorType);
   }
   // --- enable automatic drag and drop support ---
   try {
     textComponent.setDragEnabled(true);
   } catch (NoSuchMethodError e) {
     // requires JDK 1.4 ###
   }
 }
  //    public static final String showElementTreeAction = "showElementTree";
  // -------------------------------------------------------------
  public void openFile(String currDirStr, String currFileStr) {

    if (fileDialog == null) {
      fileDialog = new FileDialog(this);
    }
    fileDialog.setMode(FileDialog.LOAD);
    if (!(currDirStr.equals(""))) {
      fileDialog.setDirectory(currDirStr);
    }
    if (!(currFileStr.equals(""))) {
      fileDialog.setFile(currFileStr);
    }
    fileDialog.show();

    String file = fileDialog.getFile(); // cancel pushed
    if (file == null) {
      return;
    }
    String directory = fileDialog.getDirectory();
    File f = new File(directory, file);
    if (f.exists()) {
      Document oldDoc = getEditor().getDocument();
      if (oldDoc != null)
        // oldDoc.removeUndoableEditListener(undoHandler);
        /*
          if (elementTreePanel != null) {
          elementTreePanel.setEditor(null);
          }
        */
        getEditor().setDocument(new PlainDocument());
      fileDialog.setTitle(file);
      Thread loader = new FileLoader(f, editor1.getDocument());
      loader.start();
    }
  }
 public void setEnabled(boolean enabled) {
   textComponent.setEnabled(enabled);
   //
   if (showToolbar) {
     for (int i = 0; i < toolbarButtons.length; i++) {
       toolbarButtons[i].setEnabled(enabled);
     }
   }
 }
  /**
   * Sets the reason of a call failure if one occurs. The renderer should display this reason to the
   * user.
   *
   * @param reason the reason to display
   */
  public void setErrorReason(final String reason) {
    if (!SwingUtilities.isEventDispatchThread()) {
      SwingUtilities.invokeLater(
          new Runnable() {
            public void run() {
              setErrorReason(reason);
            }
          });
      return;
    }

    if (errorMessageComponent == null) {
      errorMessageComponent = new JTextPane();

      JTextPane textPane = (JTextPane) errorMessageComponent;
      textPane.setEditable(false);
      textPane.setOpaque(false);

      StyledDocument doc = textPane.getStyledDocument();

      MutableAttributeSet standard = new SimpleAttributeSet();
      StyleConstants.setAlignment(standard, StyleConstants.ALIGN_CENTER);
      StyleConstants.setFontFamily(standard, textPane.getFont().getFamily());
      StyleConstants.setFontSize(standard, 12);
      doc.setParagraphAttributes(0, 0, standard, true);

      GridBagConstraints constraints = new GridBagConstraints();
      constraints.fill = GridBagConstraints.HORIZONTAL;
      constraints.gridx = 0;
      constraints.gridy = 4;
      constraints.weightx = 1;
      constraints.weighty = 0;
      constraints.insets = new Insets(5, 0, 0, 0);

      add(errorMessageComponent, constraints);
      this.revalidate();
    }

    errorMessageComponent.setText(reason);

    if (isVisible()) errorMessageComponent.repaint();
  }
 // Create a Transferable implementation that contains the selected text.
 protected Transferable createTransferable(JComponent c) {
   if (c != textComponent) { // ###
     System.out.println("*** createTransferable(): c=" + c);
   }
   source = (JTextComponent) c;
   int start = source.getSelectionStart();
   int end = source.getSelectionEnd();
   Document doc = source.getDocument();
   if (start == end) {
     return null;
   }
   try {
     p0 = doc.createPosition(start);
     p1 = doc.createPosition(end);
     System.out.println(">>> createTransferable(): p0=" + p0 + ", p1=" + p1);
   } catch (BadLocationException e) {
     System.out.println(
         "*** createTransferable(): "
             + "Can't create position - unable to remove text from source.");
   }
   shouldRemove = true;
   String data = source.getSelectedText();
   return new StringSelection(data);
 }
 /**
  * @param baseURL may be <code>null</code>
  * @see PropertyPanel.PropertyField#setText
  */
 void setText(String text, String baseURL, DocumentListener documentListener) {
   //
   textComponent.getDocument().removeDocumentListener(documentListener);
   //
   if (editorType == EDITOR_TYPE_STYLED) {
     //
     // --- set content type ---
     ((JEditorPane) textComponent).setContentType("text/html");
     //
     // --- set base URL ---
     try {
       // Note: baseURL is null if no baseURL is set for the corresponding property
       // Note: baseURL is empty if corporate baseURL is used but not set
       if (baseURL != null && !baseURL.equals("")) {
         ((HTMLDocument) textComponent.getDocument()).setBase(new URL(baseURL));
       }
     } catch (MalformedURLException mue) {
       System.out.println("*** TextEditorPanel.setText(): invalid base URL: " + mue);
     }
     //
     // --- set text ---
     try {
       if (text.length() <= 59) { // ### 59 <html><head></head><body></body></html> + whitespace
         text = "<html><body><p></p></body></html>";
       }
       textComponent.setText(text);
       textComponent.setCaretPosition(0);
     } catch (Throwable e) {
       textComponent.setText(
           "<html><body><font color=#FF0000>Page can't be displayed</font></body></html>");
       System.out.println("*** TextEditorPanel.setText(): error while HTML rendering: " + e);
     }
   } else {
     textComponent.setText(text);
     textComponent.setCaretPosition(0);
   }
   //
   textComponent.getDocument().addDocumentListener(documentListener);
 }
Exemple #8
0
 /** Create an editor to represent the given document. */
 protected JTextComponent createEditor() {
   JTextComponent c = new JTextArea();
   c.setDragEnabled(true);
   c.setFont(new Font("monospaced", Font.PLAIN, 12));
   return c;
 }
 public void requestFocus() {
   textComponent.requestFocus();
 }
 String getText() {
   return textComponent.getText();
 }
  // ------------------------------------------------------------------
  // from Java Swing 1.2 Orielly - Robert Eckstein
  // ------------------------------------------------------------------
  protected JTextComponent updateKeymapForEmacs(JTextComponent textComp) {
    // note: it does not look like a key can do more than one action
    // thus no modes.
    // todo: not all of these are correct. such as ctrlK
    // todo: add saving - ctrlXS

    // create a new child keymap
    Keymap map = JTextComponent.addKeymap("NslmMap", textComp.getKeymap());

    KeyStroke selNext =
        KeyStroke.getKeyStroke(
            KeyEvent.VK_RIGHT, InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK, false);
    map.addActionForKeyStroke(selNext, getAction(DefaultEditorKit.selectionNextWordAction));

    KeyStroke selPrev =
        KeyStroke.getKeyStroke(
            KeyEvent.VK_LEFT, InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK, false);
    map.addActionForKeyStroke(selPrev, getAction(DefaultEditorKit.selectionPreviousWordAction));

    KeyStroke next = KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_MASK, false);
    map.addActionForKeyStroke(next, getAction(DefaultEditorKit.forwardAction));
    KeyStroke prev = KeyStroke.getKeyStroke(KeyEvent.VK_B, InputEvent.CTRL_MASK, false);
    map.addActionForKeyStroke(prev, getAction(DefaultEditorKit.backwardAction));

    KeyStroke selectionDown = KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK, false);
    map.addActionForKeyStroke(selectionDown, getAction(DefaultEditorKit.downAction));
    KeyStroke selectionUp = KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.CTRL_MASK, false);
    map.addActionForKeyStroke(selectionUp, getAction(DefaultEditorKit.upAction));

    KeyStroke pageDown = KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK, false);
    map.addActionForKeyStroke(pageDown, getAction(DefaultEditorKit.pageDownAction));

    KeyStroke pageUp = KeyStroke.getKeyStroke(KeyEvent.VK_U, InputEvent.CTRL_MASK, false);
    map.addActionForKeyStroke(pageUp, getAction(DefaultEditorKit.pageUpAction));

    KeyStroke endDoc =
        KeyStroke.getKeyStroke(
            KeyEvent.VK_GREATER, InputEvent.META_MASK | InputEvent.SHIFT_MASK, false);
    map.addActionForKeyStroke(endDoc, getAction(DefaultEditorKit.endAction));
    KeyStroke beginingDoc =
        KeyStroke.getKeyStroke(
            KeyEvent.VK_LESS, InputEvent.META_MASK | InputEvent.SHIFT_MASK, false);
    map.addActionForKeyStroke(beginingDoc, getAction(DefaultEditorKit.beginAction));

    // the VK_SPACE and VK_W not working as in Emacs - space deleting
    // KeyStroke
    // selectionStart=KeyStroke.getKeyStroke(KeyEvent.VK_SPACE,InputEvent.CTRL_MASK,false);
    // map.addActionForKeyStroke(selectionStart,getAction(DefaultEditorKit.selectionForwardAction));
    // //todo: setCharPosAction
    // this is doing nothing because only one char to can be assigned to cut
    // KeyStroke cut1=KeyStroke.getKeyStroke(KeyEvent.VK_W,InputEvent.CTRL_MASK,false);
    // map.addActionForKeyStroke(cut1,getAction(DefaultEditorKit.cutAction));

    // if we do save as XS, this will have to change
    KeyStroke cut = KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK, false);
    map.addActionForKeyStroke(cut, getAction(DefaultEditorKit.cutAction));

    KeyStroke paste = KeyStroke.getKeyStroke(KeyEvent.VK_Y, InputEvent.CTRL_MASK, false);
    map.addActionForKeyStroke(paste, getAction(DefaultEditorKit.pasteAction));

    KeyStroke moveToEndLine = KeyStroke.getKeyStroke(KeyEvent.VK_E, InputEvent.CTRL_MASK, false);
    map.addActionForKeyStroke(moveToEndLine, getAction(DefaultEditorKit.endLineAction));

    // not emacs like
    KeyStroke selWord = KeyStroke.getKeyStroke(KeyEvent.VK_T, InputEvent.CTRL_MASK, false);
    map.addActionForKeyStroke(selWord, getAction(DefaultEditorKit.selectWordAction));

    KeyStroke selLine = KeyStroke.getKeyStroke(KeyEvent.VK_K, InputEvent.CTRL_MASK, false);
    map.addActionForKeyStroke(selLine, getAction(DefaultEditorKit.selectLineAction));

    KeyStroke delNext = KeyStroke.getKeyStroke(KeyEvent.VK_D, InputEvent.CTRL_MASK, false);
    map.addActionForKeyStroke(delNext, getAction(DefaultEditorKit.deleteNextCharAction));

    KeyStroke insertLine = KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK, false);
    map.addActionForKeyStroke(insertLine, getAction(DefaultEditorKit.insertBreakAction));

    KeyStroke searchBackward = KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_MASK, false);
    map.addActionForKeyStroke(searchBackward, getAction("findAgain"));

    KeyStroke searchForward = KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK, false);
    map.addActionForKeyStroke(searchForward, getAction("findAgain"));

    // set the keymap for the text component
    textComp.setKeymap(map);
    return (textComp);
  } // end updateKeymapForEmacs