public void showItem() {

    if (!this.editorPanel.getViewer().isLanguageFunctionAvailable()) {

      return;
    }

    this.highlight =
        this.editorPanel
            .getEditor()
            .addHighlight(this.position, this.position + this.word.length(), null, true);

    final FindSynonymsActionHandler _this = this;

    QTextEditor editor = this.editorPanel.getEditor();

    Rectangle r = null;

    try {

      r = editor.modelToView(this.position);

    } catch (Exception e) {

      // BadLocationException!
      Environment.logError("Location: " + this.position + " is not valid", e);

      UIUtils.showErrorMessage(this.editorPanel, "Unable to display synonyms.");

      return;
    }

    int y = r.y;

    // Show a panel of all the items.
    final QPopup p = this.popup;

    p.setOpaque(false);

    Synonyms syns = null;

    try {

      syns = this.projectViewer.getSynonymProvider().getSynonyms(this.word);

    } catch (Exception e) {

      UIUtils.showErrorMessage(this.editorPanel, "Unable to display synonyms.");

      Environment.logError("Unable to lookup synonyms for: " + word, e);

      return;
    }

    if ((syns.words.size() == 0) && (this.word.toLowerCase().endsWith("ed"))) {

      // Trim off the ed and try again.
      try {

        syns = this.projectViewer.getSynonyms(this.word.substring(0, this.word.length() - 2));

      } catch (Exception e) {

        UIUtils.showErrorMessage(this.editorPanel, "Unable to display synonyms.");

        Environment.logError("Unable to lookup synonyms for: " + word, e);

        return;
      }
    }

    if ((syns.words.size() == 0) && (this.word.toLowerCase().endsWith("s"))) {

      // Trim off the ed and try again.
      try {

        syns = this.projectViewer.getSynonyms(this.word.substring(0, this.word.length() - 1));

      } catch (Exception e) {

        UIUtils.showErrorMessage(this.editorPanel, "Unable to display synonyms.");

        Environment.logError("Unable to lookup synonyms for: " + word, e);

        return;
      }
    }

    StringBuilder sb = new StringBuilder();

    if (syns.words.size() > 0) {

      sb.append("6px");

      for (int i = 0; i < syns.words.size(); i++) {

        if (sb.length() > 0) {

          sb.append(", ");
        }

        sb.append("p, 3px, [p,90px], 5px");
      }
      /*
                if (syns.words.size () > 0)
                {

                    sb.append (",5px");

                }
      */
    } else {

      sb.append("6px, p, 6px");
    }

    FormLayout summOnly = new FormLayout("3px, fill:380px:grow, 3px", sb.toString());
    PanelBuilder pb = new PanelBuilder(summOnly);

    CellConstraints cc = new CellConstraints();

    int ind = 2;

    Map<String, String> names = new HashMap();
    names.put(Synonyms.ADJECTIVE + "", "Adjectives");
    names.put(Synonyms.NOUN + "", "Nouns");
    names.put(Synonyms.VERB + "", "Verbs");
    names.put(Synonyms.ADVERB + "", "Adverbs");
    names.put(Synonyms.OTHER + "", "Other");

    if (syns.words.size() == 0) {

      JLabel l = new JLabel("No synonyms found.");
      l.setFont(l.getFont().deriveFont(Font.ITALIC));

      pb.add(l, cc.xy(2, 2));
    }

    // Determine what type of word we are looking for.
    for (Synonyms.Part i : syns.words) {

      JLabel l = new JLabel(names.get(i.type + ""));

      l.setFont(l.getFont().deriveFont(Font.ITALIC));
      l.setFont(l.getFont().deriveFont((float) UIUtils.getEditorFontSize(10)));
      l.setBorder(
          new CompoundBorder(
              new MatteBorder(0, 0, 1, 0, Environment.getBorderColor()),
              new EmptyBorder(0, 0, 3, 0)));

      pb.add(l, cc.xy(2, ind));

      ind += 2;

      HTMLEditorKit kit = new HTMLEditorKit();
      HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();

      JTextPane t = new JTextPane(doc);
      t.setEditorKit(kit);
      t.setEditable(false);
      t.setOpaque(false);

      StringBuilder buf =
          new StringBuilder(
              "<style>a { text-decoration: none; } a:hover { text-decoration: underline; }</style><span style='color: #000000; font-size: "
                  + ((int) UIUtils.getEditorFontSize(10) /*t.getFont ().getSize () + 2*/)
                  + "pt; font-family: "
                  + t.getFont().getFontName()
                  + ";'>");

      for (int x = 0; x < i.words.size(); x++) {

        String w = (String) i.words.get(x);

        buf.append("<a class='x' href='http://" + w + "'>" + w + "</a>");

        if (x < (i.words.size() - 1)) {

          buf.append(", ");
        }
      }

      buf.append("</span>");

      t.setText(buf.toString());

      t.addHyperlinkListener(
          new HyperlinkAdapter() {

            public void hyperlinkUpdate(HyperlinkEvent ev) {

              if (ev.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {

                QTextEditor ed = _this.editorPanel.getEditor();

                ed.replaceText(
                    _this.position, _this.position + _this.word.length(), ev.getURL().getHost());

                ed.removeHighlight(_this.highlight);

                _this.popup.setVisible(false);

                _this.projectViewer.fireProjectEvent(
                    ProjectEvent.SYNONYM, ProjectEvent.REPLACE, ev.getURL().getHost());
              }
            }
          });

      // Annoying that we have to do this but it prevents the text from being too small.

      t.setSize(new Dimension(380, Short.MAX_VALUE));

      JScrollPane sp = new JScrollPane(t);

      t.setCaretPosition(0);

      sp.setOpaque(false);
      sp.getVerticalScrollBar().setValue(0);
      /*
                  sp.setPreferredSize (t.getPreferredSize ());
                  sp.setMaximumSize (new Dimension (380,
                                                    75));
      */
      sp.getViewport().setOpaque(false);
      sp.setOpaque(false);
      sp.setBorder(null);
      sp.getViewport().setBackground(Color.WHITE);
      sp.setAlignmentX(Component.LEFT_ALIGNMENT);

      pb.add(sp, cc.xy(2, ind));

      ind += 2;
    }

    JPanel pan = pb.getPanel();
    pan.setOpaque(true);
    pan.setBackground(Color.WHITE);

    this.popup.setContent(pan);

    // r.y -= this.editorPanel.getScrollPane ().getVerticalScrollBar ().getValue ();

    Point po = SwingUtilities.convertPoint(editor, r.x, r.y, this.editorPanel);

    r.x = po.x;
    r.y = po.y;

    // Subtract the insets of the editorPanel.
    Insets ins = this.editorPanel.getInsets();

    r.x -= ins.left;
    r.y -= ins.top;

    this.editorPanel.showPopupAt(this.popup, r, "above", true);
  }
  @Override
  public JComponent getContent() {

    if (this.obj == null) {

      throw new IllegalStateException("No object set.");
    }

    // TODO: Make this nicer later.
    if (this.obj instanceof Chapter) {

      Chapter c = (Chapter) this.obj;

      JComponent t = UIUtils.getChapterInfoPreview(c, null, this.viewer);

      if (t == null) {

        // May be a fake chapter, return null.
        return null;
      }

      t.setSize(new Dimension(300, Short.MAX_VALUE));

      return t;

    } else {

      String firstLine = "<b><i>No description.</i></b>";

      String t = (obj.getDescription() != null ? obj.getDescription().getText() : null);

      if ((t != null) && (t.length() > 0)) {

        firstLine = new Paragraph(t, 0).getFirstSentence().getText();
      }

      JEditorPane desc = UIUtils.createHelpTextPane(firstLine, null);

      FormLayout fl = new FormLayout("380px", "p");

      PanelBuilder pb = new PanelBuilder(fl);

      CellConstraints cc = new CellConstraints();

      pb.add(desc, cc.xy(1, 1));

      desc.setAlignmentX(Component.LEFT_ALIGNMENT);

      JPanel p = pb.getPanel();
      p.setOpaque(true);
      p.setBackground(UIUtils.getComponentColor());

      return p;
    }
  }
  public FindSynonymsActionHandler(String word, int position, Chapter c, AbstractEditorPanel p) {

    this.word = word;
    this.position = position;
    // this.chapter = c;
    this.editorPanel = p;
    this.projectViewer = this.editorPanel.getViewer();

    final FindSynonymsActionHandler _this = this;

    // Show a panel of all the items.
    this.popup =
        new QPopup(
            "Synonyms for: " + word,
            Environment.getIcon(Constants.FIND_ICON_NAME, Constants.ICON_POPUP),
            null);

    JButton close =
        UIUtils.createButton(
            Constants.CLOSE_ICON_NAME, Constants.ICON_MENU, "Click to close", null);

    List<JButton> buts = new ArrayList();
    buts.add(close);

    this.popup.getHeader().setControls(UIUtils.createButtonBar(buts));

    close.addActionListener(
        new ActionAdapter() {

          public void actionPerformed(ActionEvent ev) {

            _this.popup.setVisible(false);

            _this.editorPanel.getEditor().removeHighlight(_this.highlight);
          }
        });

    p.addPopup(this.popup, true, true);
    /*
          this.highlight = this.editorPanel.getEditor ().addHighlight (position,
                                                                       position + word.length (),
                                                                       null,
                                                                       true);
    */
    this.popup.setOpaque(false);
    this.popup.setVisible(false);

    this.popup.setDraggable(this.editorPanel);
  }
예제 #4
0
  public HideablePopup(E viewer) {

    super(BoxLayout.Y_AXIS);

    this.viewer = viewer;

    this.setOpaque(true);
    this.setBackground(UIUtils.getComponentColor());
    this.setBorder(
        new CompoundBorder(
            com.quollwriter.ui.components.UIUtils.internalPanelDropShadow,
            UIUtils.createLineBorder()));

    final HideablePopup _this = this;

    this.addMouseListener(
        new MouseAdapter() {

          public void mouseEntered(MouseEvent ev) {

            if (_this.hideTimer != null) {

              _this.hideTimer.stop();
            }
          }

          public void mouseExited(MouseEvent ev) {

            if (_this.hideTimer != null) {

              Point p = new Point(0, 0);

              SwingUtilities.convertPointToScreen(p, _this);

              Rectangle tBounds = _this.getBounds(null);

              tBounds.x = p.x;
              tBounds.y = p.y;

              if (!tBounds.contains(ev.getLocationOnScreen())) {

                _this.hideTimer.start();
              }
            }
          }
        });
  }
예제 #5
0
  /**
   * Immediately shows the popup at the specified point. {@link getContent()} is called to get the
   * content of the popup.
   *
   * @param showAt Show at the specified point.
   */
  public void show(Point showAt) {

    this.removeAll();

    Box content = new Box(BoxLayout.Y_AXIS);
    UIUtils.setPadding(content, 5, 5, 5, 5);
    content.add(this.getContent());

    this.add(content);

    // For some reason we need to set the size manually.
    content.setPreferredSize(new Dimension(310, content.getPreferredSize().height));

    this.viewer.showPopupAt(this, showAt, true);
  }
  public void doInit() {

    final InviteResponseMessageBox _this = this;

    if (!this.message.isDealtWith()) {

      // Show the response.
      this.responseBox = new Box(BoxLayout.Y_AXIS);

      this.add(this.responseBox);

      JComponent l =
          UIUtils.createBoldSubHeader(
              String.format(
                  "%s the invitation", (this.message.isAccepted() ? "Accepted" : "Rejected")),
              (this.message.isAccepted()
                  ? Constants.ACCEPTED_ICON_NAME
                  : Constants.REJECTED_ICON_NAME));

      this.responseBox.add(l);
      this.responseBox.setBorder(UIUtils.createPadding(5, 5, 0, 5));

      if (this.message.isAccepted()) {

        if ((this.message.getEditorName() != null) || (this.message.getEditorAvatar() != null)) {

          JTextPane desc =
              UIUtils.createHelpTextPane(
                  "Additionally they provided the following name/avatar.", this.projectViewer);

          this.responseBox.add(Box.createVerticalStrut(5));

          this.responseBox.add(desc);
          desc.setBorder(null);
          desc.setSize(new Dimension(UIUtils.getPopupWidth() - 20, desc.getPreferredSize().height));

          Box editorInfo = new Box(BoxLayout.X_AXIS);
          editorInfo.setAlignmentX(Component.LEFT_ALIGNMENT);
          editorInfo.setBorder(UIUtils.createPadding(5, 5, 5, 5));

          this.responseBox.add(editorInfo);

          if (this.message.getEditorAvatar() != null) {

            JLabel avatar = new JLabel();

            avatar.setAlignmentY(Component.TOP_ALIGNMENT);
            avatar.setVerticalAlignment(SwingConstants.TOP);

            editorInfo.add(avatar);
            avatar.setOpaque(false);

            avatar.setIcon(
                new ImageIcon(UIUtils.getScaledImage(_this.message.getEditorAvatar(), 50)));

            avatar.setBorder(
                new CompoundBorder(UIUtils.createPadding(0, 0, 0, 5), UIUtils.createLineBorder()));
          }

          if (this.message.getEditorName() != null) {

            JLabel name = new JLabel(this.message.getEditorName());
            editorInfo.add(name);

            name.setBorder(null);
            name.setAlignmentY(Component.TOP_ALIGNMENT);
            name.setVerticalAlignment(JLabel.TOP);
            name.setAlignmentX(Component.LEFT_ALIGNMENT);
            name.setFont(
                name.getFont()
                    .deriveFont((float) UIUtils.getScaledFontSize(14))
                    .deriveFont(java.awt.Font.PLAIN));
          }
        }
      }

      final EditorEditor ed = this.message.getEditor();

      JButton ok = new JButton("Ok, got it");

      ok.addActionListener(
          new ActionAdapter() {

            public void actionPerformed(ActionEvent ev) {

              try {

                if (_this.message.isAccepted()) {

                  ed.setEditorStatus(EditorEditor.EditorStatus.current);

                  if (_this.message.getEditorName() != null) {

                    ed.setName(_this.message.getEditorName());
                  }

                  if (_this.message.getEditorAvatar() != null) {

                    ed.setAvatar(_this.message.getEditorAvatar());
                  }

                  EditorsEnvironment.updateEditor(ed);

                  // Is this response for an invite message or just out of the blue from a web
                  // service invite?
                  if (!EditorsEnvironment.hasSentMessageOfTypeToEditor(
                      ed, InviteMessage.MESSAGE_TYPE)) {

                    EditorsEnvironment.sendUserInformationToEditor(ed, null, null, null);
                  }

                } else {

                  ed.setEditorStatus(EditorEditor.EditorStatus.rejected);

                  EditorsEnvironment.updateEditor(ed);
                }

                _this.message.setDealtWith(true);

                EditorsEnvironment.updateMessage(_this.message);

                _this.responseBox.setVisible(false);

              } catch (Exception e) {

                Environment.logError("Unable to update editor: " + ed, e);

                UIUtils.showErrorMessage(
                    _this.projectViewer,
                    "Unable to update {editor}, please contact Quoll Writer support for assitance.");

                return;
              }
            }
          });

      JButton[] buts = new JButton[] {ok};

      JPanel bb = UIUtils.createButtonBar2(buts, Component.LEFT_ALIGNMENT);
      bb.setOpaque(false);
      bb.setAlignmentX(Component.LEFT_ALIGNMENT);
      bb.setBorder(UIUtils.createPadding(5, 0, 0, 0));

      this.responseBox.add(bb);

      return;
    }

    boolean accepted = this.message.isAccepted();
    String iconName = (accepted ? Constants.ACCEPTED_ICON_NAME : Constants.REJECTED_ICON_NAME);

    String message = "Accepted invitation to be {an editor}";

    if (!accepted) {

      message = "Rejected invitation to be {an editor}";
    }

    JComponent h = UIUtils.createBoldSubHeader(message, iconName);

    this.add(h);
  }
예제 #7
0
  public WizardStep getStage(String stage) {

    final WhatsNew _this = this;

    WizardStep ws = new WizardStep();

    int ind = stage.indexOf(":");

    Version v = new Version(stage.substring(0, ind));

    int lind = Integer.parseInt(stage.substring(ind + 1));

    java.util.List<WhatsNewItem> its = this.items.get(v);

    if (its == null) {

      return null;
    }

    WhatsNewItem item = its.get(lind);

    if (item == null) {

      return null;
    }

    ws.title = item.title;
    ws.helpText = this.getFirstHelpText();

    if ((item.description != null) || (item.component != null)) {

      final Box b = new Box(BoxLayout.Y_AXIS);

      if (item.description != null) {

        JTextPane hp = UIUtils.createHelpTextPane(item.description, this.projectViewer);

        hp.setBorder(null);
        hp.setSize(new Dimension(UIUtils.getPopupWidth() - 25, 500));

        Box hpb = new Box(BoxLayout.Y_AXIS);
        hpb.add(hp);
        hpb.setMaximumSize(hpb.getPreferredSize());
        hpb.setBorder(UIUtils.createPadding(0, 5, 0, 0));
        b.add(hpb);
      }

      if (item.component != null) {

        if (item.description != null) {

          b.add(Box.createVerticalStrut(5));
        }

        item.component.setAlignmentY(Component.TOP_ALIGNMENT);
        item.component.setBorder(UIUtils.createPadding(5, 10, 0, 0));

        b.add(item.component);
      }

      b.add(Box.createVerticalGlue());

      ws.panel = b;
    }

    return ws;
  }
예제 #8
0
public class Header extends Box {

  // orig: public static Color defaultPaintLeftColor = new Color (112, 149, 226);
  public static Color defaultPaintLeftColor = UIUtils.getColor("#516CA3");

  // public static Color defaultPaintLeftColor = new Color (77, 122, 244);
  public static Color defaultPaintRightColor = null;
  public static Insets defaultPadding = new Insets(3, 3, 3, 3);

  /*
  public static Font defaultFont = new Font ("Tahoma",
                                             Font.BOLD,
                                             12);
   */
  public static Color defaultTitleColor = Color.WHITE;

  private JLabel label = null;
  private PaintProvider paintProvider = null;
  private Component controls = null;

  public Header(String title, Icon icon, Component controls) {

    this();

    this.setTitle(title);
    this.setIcon(icon);

    this.setControls(controls);
  }

  public Header() {

    super(BoxLayout.LINE_AXIS);

    this.label = new JLabel();

    this.label.setAlignmentY(Component.TOP_ALIGNMENT);
    this.label.setOpaque(false);
    // this.label.setVerticalAlignment (SwingConstants.TOP);

    // this.label.setVerticalTextPosition (SwingConstants.TOP);

    this.add(this.label);
    this.add(Box.createHorizontalGlue());

    this.paintProvider =
        new GradientPainter(Header.defaultPaintLeftColor, Header.defaultPaintRightColor);

    this.setPadding(Header.defaultPadding);

    Font f = this.getFont().deriveFont(Font.BOLD, 14);

    this.setFont(f);
    this.setTitleColor(Header.defaultTitleColor);
  }

  public void setLabel(JLabel l) {

    this.label = l;
    this.label.setOpaque(false);

    this.remove(0);

    this.add(this.label, 0);

    this.validate();
    this.repaint();
  }

  public Component getControls() {

    return this.controls;
  }

  public void setControls(Component c) {

    if (c == null) {

      return;
    }

    if (c instanceof JComponent) {

      ((JComponent) c).setAlignmentY(Component.TOP_ALIGNMENT);
    }

    int ind = -1;

    if (this.controls != null) {

      for (int i = 0; i < this.getComponentCount(); i++) {

        if (this.getComponent(i) == this.controls) {

          ind = i;

          break;
        }
      }
    }

    if (ind > -1) {

      this.remove(ind);

      this.add(c, ind);

    } else {

      this.add(c);
    }

    this.controls = c;

    this.validate();
    this.repaint();
  }

  public JLabel getLabel() {

    return this.label;
  }

  public void setPadding(Insets i) {

    if (i == null) {

      this.setBorder(null);

      return;
    }

    this.setBorder(new EmptyBorder(i));
  }

  public Header getClone(String title, Icon icon, Component controls) {

    Header n = new Header(title, icon, controls);

    n.setTitleColor(this.label.getForeground());
    n.setPaintProvider(this.getPaintProvider().getClone());
    n.setFont(this.label.getFont());

    return n;
  }

  public Dimension getMaximumSize() {

    return new Dimension(Short.MAX_VALUE, this.getPreferredSize().height);
  }

  public Dimension getMinimumSize() {

    return new Dimension(
        this.label.getPreferredSize().width
            + 5
            + ((this.controls != null) ? this.controls.getMinimumSize().width : 0),
        this.getPreferredSize().height);
  }

  public PaintProvider getPaintProvider() {

    return this.paintProvider;
  }

  public void setPaintProvider(PaintProvider p) {

    this.paintProvider = p;
  }

  public String getTitle() {

    return this.label.getText();
  }

  public Icon getIcon() {

    return this.label.getIcon();
  }

  public void setIcon(Icon i) {

    this.label.setIcon(i);
  }

  public void setTitleColor(Color c) {

    this.label.setForeground(c);
  }

  public void setTitle(String t) {

    this.label.setText(
        String.format("<html>%s</html>", com.quollwriter.Environment.replaceObjectNames(t)));
  }

  public Font getFont() {

    return this.label.getFont();
  }

  public void setFont(Font f) {

    this.label.setFont(f);
  }

  public void setFontSize(float i) {

    this.label.setFont(this.label.getFont().deriveFont(i));
  }

  public void setBold(boolean b) {

    this.label.setFont(this.label.getFont().deriveFont((b ? Font.BOLD : Font.PLAIN)));
  }

  protected void paintComponent(Graphics g) {

    if (this.paintProvider != null) {

      Paint p = this.paintProvider.getPaint(this);

      Graphics2D g2d = (Graphics2D) g;

      int w = this.getWidth();
      int h = this.getHeight();

      g2d.setPaint(p);
      g2d.fillRect(0, 0, w, h);

      this.setOpaque(false);
    }

    super.paintComponent(g);

    // this.setOpaque (true);

  }

  public static Header createBoldSubHeader(String title, ImageIcon icon) {

    Header h = new Header(title, ((icon == null) ? null : icon), null);

    h.setFont(h.getFont().deriveFont(Font.BOLD, 12f));
    // h.setTitleColor (Color.BLACK);
    // h.setOpaque (false);
    // h.setBackground (new Color (0, 0, 0, 0));
    // h.setPaintProvider (null);

    h.setAlignmentX(Component.LEFT_ALIGNMENT);

    h.setBorder(new EmptyBorder(3, 3, 3, 3));

    return h;
  }
}