/**
  * A convience method to read a font from a style.
  *
  * @param style Style being modified
  * @return The font inside the passed style
  */
 public static String getFontString(Style style) {
   String font = StyleConstants.getFontFamily(style) + "-";
   if (StyleConstants.isBold(style)) font += "BOLD";
   if (StyleConstants.isItalic(style)) font += "ITALIC";
   if (!StyleConstants.isBold(style) && !StyleConstants.isItalic(style)) font += "PLAIN";
   font += "-" + StyleConstants.getFontSize(style);
   return font;
 }
Example #2
0
  // ----------------------------------------------------------------------------
  public void setAttributes(AttributeSet a) {
    attributes_ = new SimpleAttributeSet(a);
    String name = StyleConstants.getFontFamily(a);
    font_names_.setSelected(name);
    int size = StyleConstants.getFontSize(a);
    font_sizes_.setSelectedInt(size);

    updatePreview();
  }
Example #3
0
  public void actionPerformed(ActionEvent e) {
    if (!activeEvent) {
      activeEvent = true;
      return;
    }
    if (getSelectedItem().equals(other)) {
      int currentSize = StyleConstants.getFontSize(attr);
      hidePopup();
      currentSize = FontSizeOption.showAsDialog(this, currentSize);
      activeEvent = FontSizeOption.getResult();

      this.setSelectedItem(new Integer(currentSize));
    } else {
      ActionListener[] l = this.getActionListeners();
      for (int i = 0; i < l.length; i++) {
        if (l[i] == this) continue;
        l[i].actionPerformed(e);
      }
    }
  }
 /**
  * Modify the current base font size
  *
  * @param s the size to add to the current size
  */
 public void modifyFont(final int s) {
   try {
     currentFontSize = Math.min(Math.max(0, currentFontSize + s), 6);
     HTMLDocument doc = (HTMLDocument) accessibleHtml.getDocument();
     StyleContext.NamedStyle style =
         (StyleContext.NamedStyle) doc.getStyleSheet().getStyle("body");
     MutableAttributeSet attr = (MutableAttributeSet) style.getResolveParent();
     if (StyleConstants.getFontSize(attr) != fontSizes[currentFontSize]) {
       ConfigManager.setHelpFontSize(currentFontSize);
       StyleConstants.setFontSize(attr, fontSizes[currentFontSize]);
       accessibleHtml.setVisible(false);
       style.setResolveParent(attr);
       accessibleHtml.setVisible(true);
     }
   } catch (NullPointerException e) {
     // Can occur if the user is changing quickly the document
     SwingUtilities.invokeLater(
         () -> {
           modifyFont(s);
         });
   }
 }
  public void actionPerformed(ActionEvent e) {
    JTextPane editor = (JTextPane) getEditor(e);
    int p0 = editor.getSelectionStart();
    StyledDocument doc = getStyledDocument(editor);
    Element paragraph = doc.getCharacterElement(p0);
    AttributeSet as = paragraph.getAttributes();

    family = StyleConstants.getFontFamily(as);
    fontSize = StyleConstants.getFontSize(as);

    formatText = new JDialog(new JFrame(), "Font and Size", true);
    formatText.getContentPane().setLayout(new BorderLayout());

    JPanel choosers = new JPanel();
    choosers.setLayout(new GridLayout(2, 1));

    JPanel fontFamilyPanel = new JPanel();
    fontFamilyPanel.add(new JLabel("Font"));

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    String[] fontNames = ge.getAvailableFontFamilyNames();

    fontFamilyChooser = new JComboBox<String>();
    for (int i = 0; i < fontNames.length; i++) {
      fontFamilyChooser.addItem(fontNames[i]);
    }
    fontFamilyChooser.setSelectedItem(family);
    fontFamilyPanel.add(fontFamilyChooser);
    choosers.add(fontFamilyPanel);

    JPanel fontSizePanel = new JPanel();
    fontSizePanel.add(new JLabel("Size"));
    fontSizeChooser = new JComboBox<Float>();
    fontSizeChooser.setEditable(true);
    for (int size = 2; size <= 60; size += 2) {
      fontSizeChooser.addItem(new Float(size));
    }
    fontSizeChooser.setSelectedItem(new Float(fontSize));
    fontSizePanel.add(fontSizeChooser);
    choosers.add(fontSizePanel);

    JButton ok = new JButton("OK");
    ok.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
            accept = true;
            formatText.dispose();
            family = (String) fontFamilyChooser.getSelectedItem();
            fontSize = Float.parseFloat(fontSizeChooser.getSelectedItem().toString());
          }
        });

    JButton cancel = new JButton("Cancel");
    cancel.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
            formatText.dispose();
          }
        });

    JPanel buttons = new JPanel();
    buttons.add(ok);
    buttons.add(cancel);
    formatText.getContentPane().add(choosers, BorderLayout.CENTER);
    formatText.getContentPane().add(buttons, BorderLayout.SOUTH);
    formatText.pack();
    formatText.setVisible(true);

    MutableAttributeSet attr = null;
    if (editor != null && accept) {
      attr = new SimpleAttributeSet();
      StyleConstants.setFontFamily(attr, family);
      StyleConstants.setFontSize(attr, (int) fontSize);
      setCharacterAttributes(editor, attr, false);
    }
  }
Example #6
0
 public void setAttributes(AttributeSet attr) {
   int sz = StyleConstants.getFontSize(attr);
   this.attr = attr;
   activeEvent = false;
   this.setSelectedItem(new Integer(sz));
 }