Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
SearchDialog.java
1 
16 package net.squiz.matrix.ui;
17 
18 import net.squiz.matrix.plaf.MatrixLookAndFeel;
19 import net.squiz.matrix.matrixtree.*;
20 import net.squiz.matrix.core.*;
21 import javax.swing.*;
22 import java.awt.event.*;
23 import java.awt.*;
24 import javax.swing.border.*;
25 import java.util.*;
26 
27 public class SearchDialog extends MatrixDialog {
28 
29  private JButton searchButton;
30  private JButton cancelButton;
31  private JTextField searchTerm;
32  private JLabel message;
33  private static Point prevScreenLocation = null;
34 
35  private SearchDialog(Point locationOnScreen, Dimension treeDimension) {
36  JPanel contentPane = new JPanel(new BorderLayout());
37  setContentPane(contentPane);
38  contentPane.setBorder(new EmptyBorder(1, 1, 1, 1));
39 
40  contentPane.add(getTopPanel(Matrix.translate("asset_map_dialog_jump")),BorderLayout.NORTH);
41 
42  contentPane.add(getSearcFormPanel());
43  contentPane.setBackground(MatrixLookAndFeel.PANEL_BORDER_COLOUR);
44  enableDrag(contentPane);
45  }
46 
47 
48  public void dispose() {
49  prevScreenLocation = getPrevLoc();
50  super.dispose();
51  }
52 
58  public static SearchDialog getSearchDialog(Point locationOnScreen, Dimension treeDimension) {
59 
60  SearchDialog searchDialog = null;
61  if (!MatrixDialog.hasDialog(SearchDialog.class)) {
62  searchDialog = new SearchDialog(locationOnScreen, treeDimension);
63  MatrixDialog.putDialog(searchDialog);
64  } else {
65  // bring the seach dialog to the front
66  searchDialog = (SearchDialog) MatrixDialog.getDialog(SearchDialog.class);
67  searchDialog.toFront();
68  }
69 
70  searchDialog.pack();
71  searchDialog.centerDialogOnTree(locationOnScreen, treeDimension, prevScreenLocation);
72 
73  return searchDialog;
74  }
75 
76 
77  private JPanel getMessagePanel() {
78  JPanel messagePanel = new JPanel();
79  message = new JLabel("");
80  messagePanel.add(message);
81 
82  return messagePanel;
83  }
84 
85  private JPanel getSearcFormPanel() {
86  // create a form panel
87  JPanel searchForm = new JPanel();
88  searchForm.setBackground(MatrixLookAndFeel.PANEL_COLOUR);
89 
90  // Text field settings
91  searchTerm = new JTextField(5);
92  searchTerm.setPreferredSize(new Dimension(150,15));
93  searchTerm.setFont(MatrixTreeBus.getActiveTree().getFontInUse());
94  searchTerm.setOpaque(false);
95  searchTerm.addKeyListener(new KeyListener());
96  searchTerm.setBorder( new LineBorder(MatrixLookAndFeel.PANEL_BORDER_COLOUR, 1) );
97 
98  // Label settings
99  //JLabel label = new JLabel("Jump to ");
100  //label.setFont(new Font("Arial",Font.PLAIN, 10));
101 
102  // add components to formPanel
103  //searchForm.add(label);
104  searchForm.add(searchTerm);
105 
106  return searchForm;
107  }
108 
109  class ButtonHandler implements ActionListener {
110  public void actionPerformed(ActionEvent evt) {
111  Object source = evt.getSource();
112 
113  if (source == searchButton) {
114  try {
115  MatrixTree tree = MatrixTreeBus.getActiveTree();
116  MatrixTreeNode[] nodes = tree.getSelectionNodes();
117  tree.loadChildAssets(nodes[0],"",Integer.parseInt(searchTerm.getText())-1,-1);
118  dispose();
119  } catch (NullPointerException ex) {
120  message.setText(Matrix.translate("asset_map_error_invalid_node"));
121  } catch (NumberFormatException ex) {
122  message.setText(Matrix.translate("asset_map_error_invalid_number"));
123  } catch (Exception ex) {
124  Object[] transArgs = {
125  ex.getMessage()
126  };
127  message.setText(Matrix.translate("asset_map_error_unknown_error", transArgs));
128  }
129  } else if (source == cancelButton) {
130  dispose();
131  }
132  }
133  }
134 
135  private class KeyListener extends KeyAdapter {
136  public void keyPressed(KeyEvent evt) {
137  if (evt.getSource() == searchTerm) {
138  if (evt.getKeyCode() == evt.VK_ENTER) {
139  MatrixTree tree = MatrixTreeBus.getActiveTree();
140  MatrixTreeNode[] nodes = tree.getSelectionNodes();
141  if (nodes[0].getAsset().getNumKids() >= (Integer.parseInt(searchTerm.getText())-1)) {
142  tree.loadChildAssets(nodes[0],"",Integer.parseInt(searchTerm.getText())-1,-1);
143  }
144  dispose();
145  }else if(evt.getKeyCode() == evt.VK_ESCAPE) {
146  dispose();
147  }
148  }
149  }
150  }
151 }
152