Skip to content

Commit

Permalink
nacharbeiten: DemoFindPanel extends JXFindPanel für SearchDemo
Browse files Browse the repository at this point in the history
homebeaver/SwingSet#38

SearchDemo hat damit drei Möglichkeiten zu Suchen
- NORTH : JXFindBar searchPanel ( wie beísher )
- SHIFT_DOWN+F5 cmdFind : JXFindPanel als Dialog ( wie beísher )
- (neu) SOUTH : DemoFindPanel findPanel mit JXSearchField
  • Loading branch information
homebeaver committed Nov 3, 2022
1 parent 2cd5eee commit ad4c140
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 6 deletions.
138 changes: 138 additions & 0 deletions src/main/java/org/jdesktop/swingx/demos/search/DemoFindPanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package org.jdesktop.swingx.demos.search;

import java.awt.Color;
import java.awt.FlowLayout;
import java.util.logging.Logger;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

import org.jdesktop.swingx.JXDialog;
import org.jdesktop.swingx.JXFindPanel;
import org.jdesktop.swingx.JXSearchField;
import org.jdesktop.swingx.demos.svg.FeatheRinfo;
import org.jdesktop.swingx.icon.RadianceIcon;
import org.jdesktop.swingx.search.Searchable;

/**
* A FindPanel which uses JXSearchField.
*
* @author homeb
*/
@SuppressWarnings("serial")
public class DemoFindPanel extends JXFindPanel {

private static final Logger LOG = Logger.getLogger(DemoFindPanel.class.getName());

protected JButton findNext;
protected JButton findPrevious;

public DemoFindPanel() {
this(null);
}

public DemoFindPanel(Searchable searchable) {
super(searchable);
}

/**
* {@inheritDoc} <p>
* Overridden to replace informationIcon in <code>JOptionPane.showMessageDialog</code>
* and use message containing bold searchString
*/
/* code in super JXFindPanel:
JOptionPane.showMessageDialog(this, getUIString("notFound"));
uses: "OptionPane.informationIcon" // ... , blue circle with i
replace with "org.jdesktop.swingx.demos.svg.FeatheRinfo"
*/
@Override
protected void showNotFoundMessage() {
RadianceIcon informationIcon = FeatheRinfo.of(RadianceIcon.BUTTON_ICON, RadianceIcon.BUTTON_ICON);
informationIcon.setColorFilter(color -> Color.BLUE);
String msg = "<html>" + getUIString("notFound") + ":<font color=red><b> "+searchField.getText()+"</b></font></html>";
JOptionPane.showMessageDialog
( this // parentComponent
// , getUIString("notFound") // message
, msg
, UIManager.getString("OptionPane.messageDialogTitle", getLocale()) // String title, nls : "Meldung" / Message
, JOptionPane.INFORMATION_MESSAGE
, informationIcon
);
}

@Override
// wie in JXFindBar;
protected void bind() {
super.bind();
searchField.addActionListener(getAction(JXDialog.EXECUTE_ACTION_COMMAND));
findNext.setAction(getAction(FIND_NEXT_ACTION_COMMAND));
findPrevious.setAction(getAction(FIND_PREVIOUS_ACTION_COMMAND));
KeyStroke stroke = KeyStroke.getKeyStroke("ESCAPE");
getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(stroke, JXDialog.CLOSE_ACTION_COMMAND);
}

@Override
// wie in JXFindBar;
protected void build() {
setLayout(new FlowLayout(SwingConstants.LEADING));
add(searchLabel);
add(new JLabel(":"));
add(new JLabel(" "));
add(searchField);
add(findNext);
add(findPrevious);

add(matchCheck); // Case sensitiv / Groß/Kleinschreibung
add(wrapCheck); // Wrap Seach
// add(backCheck); // Backward / Rückwärts
}

private static final String SEARCHFIELD_PROMPT = "Enter a search text here";
/**
* {@inheritDoc} <p>
* Overridden to use <code>JXSearchField</code> with "textHighlight" Background.
* This color is defined in BasicLookAndFeel.initSystemColorDefaults : Text background color when selected
*/
@Override
protected void initComponents() {
/* in AbstractPatternPanel
searchLabel = new JLabel();
searchField = new JTextField(getSearchFieldWidth()) { <==== das will ich überschreiben
@Override
public Dimension getMaximumSize() {
Dimension superMax = super.getMaximumSize();
superMax.height = getPreferredSize().height;
return superMax;
}
};
matchCheck = new JCheckBox();
*/
/* in JXFindPanel:
super.initComponents();
wrapCheck = new JCheckBox();
backCheck = new JCheckBox();
*/
searchLabel = new JLabel();
searchField = new JXSearchField(SEARCHFIELD_PROMPT);
searchField.setColumns(super.getSearchFieldWidth());
// LOG.info(">>>>>>>>>>>searchField:"+searchField);
// LOG.info("searchField.Background:"+searchField.getBackground());
// if(searchField.getBackground().getRGB()==-16777216) { // BLACK
searchField.setBackground(UIManager.getColor("textHighlight"));
LOG.info("searchField.Background:"+searchField.getBackground());
// }
matchCheck = new JCheckBox();

wrapCheck = new JCheckBox();
backCheck = new JCheckBox();
// wie in JXFindBar;
findNext = new JButton();
findPrevious = new JButton();
}

}
41 changes: 35 additions & 6 deletions src/main/java/org/jdesktop/swingx/demos/search/SearchDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

import org.jdesktop.beans.AbstractBean;
import org.jdesktop.swingx.JXFindBar;
import org.jdesktop.swingx.JXFindPanel;
import org.jdesktop.swingx.JXFrame;
import org.jdesktop.swingx.JXFrame.StartPosition;
import org.jdesktop.swingx.JXList;
Expand Down Expand Up @@ -84,7 +85,7 @@ public class SearchDemo extends AbstractDemo {
private static final long serialVersionUID = 3818043478721123293L;
private static final Logger LOG = Logger.getLogger(SearchDemo.class.getName());
private static final String DESCRIPTION = "Demonstrates base searching functionality plus custom configuration.";

private static final String SEARCHFIELD_PROMPT = "Enter a search text here";
/**
* main method allows us to run as a standalone demo.
* @param args params
Expand Down Expand Up @@ -118,6 +119,8 @@ public void run() {
private JXList<Object> list;
private JXTable table;

// private JXSearchField searchField;
private JXFindPanel findPanel;
private JXFindBar searchPanel;
private Map<String, StringValue> stringValues;
private SearchControl searchControl;
Expand Down Expand Up @@ -276,16 +279,18 @@ public void run() {
* Looks for a Searchable in the potential searchable provider and
* sets it as the current searchable of the search panel.
*
* @param searchableProvider a component which
* @param searchableProvider a component (JXTable, ... ) which
* @return the Searchable for searchableProvider
*/
protected void updateSearchPanel(Object searchableProvider) {
protected Searchable updateSearchPanel(Object searchableProvider) {
final Searchable s = getSearchable(searchableProvider);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
searchPanel.setSearchable(s);
KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent(searchPanel);
}
});
return s;
}


Expand All @@ -294,13 +299,14 @@ public void run() {
* and updates the searchPanel accordingly.
*
* @param tabbed JTabbedPane
* @return the searchable
*/
protected void updateSearchable(JTabbedPane tabbed) {
protected Searchable updateSearchable(JTabbedPane tabbed) {
Component comp = tabbed.getSelectedComponent();
if (comp instanceof JScrollPane) {
comp = (JComponent) ((JScrollPane) comp).getViewport().getView();
}
updateSearchPanel(comp);
return updateSearchPanel(comp);
}


Expand Down Expand Up @@ -436,7 +442,6 @@ private class SearchControl extends AbstractBean {
private HashMap<String, ColorHighlighter> colorCellMarkers;

public SearchControl() {
// initMatchMarkers(); :
createMatchingTextMarkers();
createColorCellMarkers();
installMatchMarkers(colorCellMarkers);
Expand Down Expand Up @@ -559,6 +564,30 @@ private JTabbedPane initComponents() {
addTab(tabbedPane, list, "listTabTitle", true);
addTab(tabbedPane, tree, "treeTabTitle", true);
addTab(tabbedPane, treeTable, "treeTableTabTitle", true);

// findPanel with searchField
findPanel = new DemoFindPanel(updateSearchable(tabbedPane));
// searchField = new JXSearchField(SEARCHFIELD_PROMPT);
// // In REGULAR search mode, an action event is fired,
// // when the user presses enter or clicks the find button.
// searchField.setSearchMode(JXSearchField.SearchMode.REGULAR);
// searchField.addActionListener(actionEvent -> {
// LOG.info("actionEvent["+actionEvent.getClass().getSimpleName()+"]:"+actionEvent.getActionCommand()
// +", ModifiersExText("+actionEvent.getModifiers()+")="+InputEvent.getModifiersExText(actionEvent.getModifiers())
// +(actionEvent.getModifiers()==0 ? " ENTER" : " find button pushed")
// );
// String searchString = actionEvent.getActionCommand();
// // TODO search
// Searchable s = updateSearchable(tabbedPane);
// if(searchString.isEmpty()) {
// // nothing to do
// } else {
// int matchIdx = s.search(searchString);
// LOG.info("\""+searchString+"\" in Searchable:"+s + " results to "+matchIdx);
// }
// });
add(findPanel, BorderLayout.SOUTH);

return tabbedPane;
}

Expand Down

0 comments on commit ad4c140

Please sign in to comment.