Пример #1
0
  private void updatePopupLocation() {
    final Point los = WebColorChooserField.this.getLocationOnScreen();
    final Rectangle gb = popup.getGraphicsConfiguration().getBounds();
    final int shadeWidth = isDrawBorder() ? getShadeWidth() : 0;
    final boolean ltr = WebColorChooserField.this.getComponentOrientation().isLeftToRight();
    final int w = WebColorChooserField.this.getWidth();
    final int h = WebColorChooserField.this.getHeight();

    final int x;
    if (ltr) {
      if (los.x + shadeWidth + popup.getWidth() <= gb.x + gb.width) {
        x = los.x + shadeWidth;
      } else {
        x = los.x + w - shadeWidth - popup.getWidth();
      }
    } else {
      if (los.x + w - shadeWidth - popup.getWidth() >= gb.x) {
        x = los.x + w - shadeWidth - popup.getWidth();
      } else {
        x = los.x + shadeWidth;
      }
    }

    final int y;
    if (los.y + h + popup.getHeight() <= gb.y + gb.height) {
      y = los.y + h + (isDrawBorder() ? 0 : 1);
    } else {
      y = los.y - popup.getHeight() - (isDrawBorder() ? 0 : 1);
    }

    popup.setLocation(x, y);
  }
Пример #2
0
  /** Color chooser popup */
  private void showColorChooserPopup() {
    // Checking that component is eligable for focus request
    if (!requestFocusInWindow() && !isFocusOwner()) {
      // Cancel operation if component is not eligable for focus yet
      // This might occur if some other component input verifier holds the focus or in some other
      // rare cases
      return;
    }

    // Update date from field if it was changed
    updateColorFromField();

    // Create popup if it doesn't exist
    if (popup == null || colorChooserPanel == null) {
      final Window ancestor = SwingUtils.getWindowAncestor(this);

      // Color chooser
      colorChooserPanel = new WebColorChooserPanel(true);
      colorChooserPanel.setColor(color);
      colorChooserPanel.setUndecorated(false);
      colorChooserPanel.setPaintFocus(false);
      colorChooserPanel.setRound(StyleConstants.smallRound);
      colorChooserPanel.setShadeWidth(0);

      // Popup window
      popup = new WebWindow(ancestor);
      popup.setLayout(new BorderLayout());
      popup.setCloseOnFocusLoss(true);
      popup.setWindowOpaque(false);
      popup.add(colorChooserPanel);
      popup.pack();

      // Correct popup positioning
      updatePopupLocation();
      ancestor.addComponentListener(
          new ComponentAdapter() {
            @Override
            public void componentMoved(final ComponentEvent e) {
              if (popup.isShowing()) {
                updatePopupLocation();
              }
            }

            @Override
            public void componentResized(final ComponentEvent e) {
              if (popup.isShowing()) {
                updatePopupLocation();
              }
            }
          });
      ancestor.addPropertyChangeListener(
          WebLookAndFeel.ORIENTATION_PROPERTY,
          new PropertyChangeListener() {
            @Override
            public void propertyChange(final PropertyChangeEvent evt) {
              if (popup.isShowing()) {
                updatePopupLocation();
              }
            }
          });

      colorChooserPanel.addColorChooserListener(
          new ColorChooserListener() {
            @Override
            public void okPressed(final ActionEvent e) {
              setColor(colorChooserPanel.getColor());
              popup.setVisible(false);
            }

            @Override
            public void resetPressed(final ActionEvent e) {}

            @Override
            public void cancelPressed(final ActionEvent e) {
              popup.setVisible(false);
            }
          });
    } else {
      // Updating window location
      updatePopupLocation();

      // Updating color
      colorChooserPanel.setColor(color);
    }

    // Applying orientation to popup
    SwingUtils.copyOrientation(WebColorChooserField.this, popup);

    // Showing popup and changing focus
    popup.setVisible(true);
    colorChooserPanel.requestFocusInWindow();
  }
Пример #3
0
 private void hideHints() {
   if (classSearchHintsPopup != null) {
     classSearchHintsPopup.setVisible(false);
   }
 }
Пример #4
0
 private void updateHintsLocation() {
   Point p = classSearchField.getLocationOnScreen();
   classSearchHintsPopup.setLocation(p.x, p.y + classSearchField.getHeight() + 2);
 }
Пример #5
0
  private void updateHints() {
    String text = classSearchField.getText().trim();

    // Ignore empty text
    if (text.trim().length() == 0) {
      hideHints();
      return;
    }

    // Updating hints window if needed
    if (!CompareUtils.equals(lastSearchedText, text)) {
      // Saving old selection
      Object oldSelection = classSearchHintsList.getSelectedValue();

      // Clearing list
      DefaultListModel model = (DefaultListModel) classSearchHintsList.getModel();
      model.clear();

      // Look for classes
      List<JarEntry> found =
          jarStructure.findSimilarEntries(
              text,
              new Filter<JarEntry>() {
                @Override
                public boolean accept(JarEntry object) {
                  return object.getType().equals(JarEntryType.javaEntry);
                }
              });
      if (found.size() > 0) {
        classSearchField.setForeground(Color.BLACK);

        // Filling list with results
        for (JarEntry entry : found) {
          model.addElement(entry);
        }

        // Updating visible rows
        classSearchHintsList.setVisibleRowCount(Math.min(model.size(), 10));

        // Restoring selection if possible
        int index = oldSelection != null ? model.indexOf(oldSelection) : 0;
        classSearchHintsList.setSelectedIndex(index != -1 ? index : 0);

        // Packing popup
        classSearchHintsPopup.pack();

        // Displaying hints window
        if (!classSearchHintsPopup.isVisible()) {
          classSearchHintsPopup.setVisible(true);
        }
      } else {
        classSearchField.setForeground(Color.RED);

        // Hiding hints window
        if (classSearchHintsPopup.isVisible()) {
          classSearchHintsPopup.setVisible(false);
        }
      }

      lastSearchedText = text;
    }
  }
Пример #6
0
  private WebButton createClassSearch() {
    WebButton classSearch = new WebButton(classSearchIcon);
    classSearch.setDrawFocus(false);
    classSearch.setRolloverDecoratedOnly(true);
    classSearch.addHotkey(Hotkey.CTRL_N);
    classSearch.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            showClassSearchPopup();
          }
        });

    classSearchField = new WebTextField(20, false);
    classSearchField.setHideInputPromptOnFocus(false);
    classSearchField.setInputPrompt("Enter class name here...");
    HotkeyManager.registerHotkey(
        classSearchField,
        Hotkey.ESCAPE,
        new HotkeyRunnable() {
          @Override
          public void run(KeyEvent e) {
            hideClassSearchPopup();
          }
        });

    final WebImage leadingComponent = new WebImage(classSearchIcon);
    leadingComponent.setMargin(2);
    classSearchField.setLeadingComponent(leadingComponent);

    classSearchPopup = new WebPopup();
    classSearchPopup.setCloseOnFocusLoss(true);
    classSearchPopup.add(classSearchField);
    classSearchPopup.setDefaultFocusComponent(classSearchField);

    classSearchHintsPopup =
        new WebWindow(classSearchPopup) {
          @Override
          public Dimension getPreferredSize() {
            final Dimension ps = super.getPreferredSize();
            ps.width = Math.max(classSearchField.getWidth(), ps.width);
            return ps;
          }
        };
    classSearchHintsPopup.setFocusable(false);
    classSearchHintsPopup.setAlwaysOnTop(true);
    classSearchPopup.addFocusableChild(classSearchHintsPopup);

    classSearchHintsList = new WebList(new DefaultListModel());
    classSearchHintsList.setFocusable(false);
    classSearchHintsList.setRolloverSelectionEnabled(true);
    classSearchHintsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    classSearchHintsList.setCellRenderer(
        new WebListCellRenderer() {
          @Override
          public Component getListCellRendererComponent(
              JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            JarEntry entry = (JarEntry) value;
            WebListElement renderer =
                (WebListElement)
                    super.getListCellRendererComponent(
                        list, value, index, isSelected, cellHasFocus);
            renderer.setIcon(entry.getIcon());
            renderer.setText(entry.getName());
            return renderer;
          }
        });
    classSearchHintsList.addMouseListener(
        new MouseAdapter() {
          @Override
          public void mousePressed(MouseEvent e) {
            if (SwingUtils.isLeftMouseButton(e)) {
              openSelectedHint();
            }
          }
        });

    HotkeyManager.registerHotkey(
        classSearchField,
        Hotkey.HOME,
        new HotkeyRunnable() {
          @Override
          public void run(KeyEvent e) {
            if (classSearchHintsList.getModelSize() > 0) {
              classSearchHintsList.setSelectedIndex(0);
            }
          }
        });
    HotkeyManager.registerHotkey(
        classSearchField,
        Hotkey.UP,
        new HotkeyRunnable() {
          @Override
          public void run(KeyEvent e) {
            if (classSearchHintsList.getModelSize() > 0) {
              int index = classSearchHintsList.getSelectedIndex();
              if (index > 0) {
                classSearchHintsList.setSelectedIndex(index - 1);
              } else {
                classSearchHintsList.setSelectedIndex(classSearchHintsList.getModelSize() - 1);
              }
            }
          }
        });
    HotkeyManager.registerHotkey(
        classSearchField,
        Hotkey.DOWN,
        new HotkeyRunnable() {
          @Override
          public void run(KeyEvent e) {
            if (classSearchHintsList.getModelSize() > 0) {
              int index = classSearchHintsList.getSelectedIndex();
              if (index < classSearchHintsList.getModelSize() - 1) {
                classSearchHintsList.setSelectedIndex(index + 1);
              } else {
                classSearchHintsList.setSelectedIndex(0);
              }
            }
          }
        });
    HotkeyManager.registerHotkey(
        classSearchField,
        Hotkey.END,
        new HotkeyRunnable() {
          @Override
          public void run(KeyEvent e) {
            if (classSearchHintsList.getModelSize() > 0) {
              classSearchHintsList.setSelectedIndex(classSearchHintsList.getModelSize() - 1);
            }
          }
        });
    HotkeyManager.registerHotkey(
        classSearchField,
        Hotkey.ENTER,
        new HotkeyRunnable() {
          @Override
          public void run(KeyEvent e) {
            openSelectedHint();
          }
        });

    WebScrollPane foundClassesScroll = new WebScrollPane(classSearchHintsList);
    foundClassesScroll.setShadeWidth(0);
    foundClassesScroll.setRound(0);
    classSearchHintsPopup.add(foundClassesScroll);

    classSearchPopup.addComponentListener(
        new ComponentAdapter() {
          @Override
          public void componentMoved(ComponentEvent e) {
            updateHintsLocation();
          }

          @Override
          public void componentResized(ComponentEvent e) {
            updateHintsLocation();
          }
        });

    classSearchPopup.addPopupListener(
        new PopupAdapter() {
          @Override
          public void popupWillBeOpened() {
            lastSearchedText = null;
            lastFocusBeforeSearch = FocusManager.getFocusOwner();
          }

          @Override
          public void popupOpened() {
            updateHints();
          }

          @Override
          public void popupClosed() {
            hideHints();
            if (lastFocusBeforeSearch != null) {
              lastFocusBeforeSearch.requestFocusInWindow();
            }
          }
        });

    classSearchField.addCaretListener(
        new CaretListener() {
          @Override
          public void caretUpdate(CaretEvent e) {
            if (classSearchHintsDelay == null) {
              classSearchHintsDelay =
                  new WebTimer(
                      500,
                      new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                          updateHints();
                        }
                      });
              classSearchHintsDelay.setRepeats(false);
            }
            if (classSearchField.getText().trim().length() > 0) {
              classSearchHintsDelay.restart();
            } else {
              classSearchHintsDelay.stop();
              hideHints();
            }
          }
        });

    return classSearch;
  }