Mercurial Hosting > junotu
comparison src/junotu/TabSimpleSearch.java @ 103:7a3fd865654a
Spacing/Formatting/Indentation: Now using tabs exclusively, instead of a mix of tabs and spaces
author | Fox |
---|---|
date | Thu, 06 Apr 2023 01:44:10 +0200 |
parents | 50af17182cd8 |
children | e4588b8a8ddc |
comparison
equal
deleted
inserted
replaced
102:a313eaea437a | 103:7a3fd865654a |
---|---|
33 import junotu.Card; | 33 import junotu.Card; |
34 import junotu.CardWidget; | 34 import junotu.CardWidget; |
35 | 35 |
36 public class TabSimpleSearch extends JPanel implements ActionListener, TabInterface { | 36 public class TabSimpleSearch extends JPanel implements ActionListener, TabInterface { |
37 | 37 |
38 public boolean dirty; | 38 public boolean dirty; |
39 | 39 |
40 private final String KEY_ACTION_COMMIT = "commit"; | 40 private final String KEY_ACTION_COMMIT = "commit"; |
41 | 41 |
42 private JTextField field; | 42 private JTextField field; |
43 private JButton create; | 43 private JButton create; |
44 private JButton context; | 44 private JButton context; |
45 private JPopupMenu menu; | 45 private JPopupMenu menu; |
46 private Box results; | 46 private Box results; |
47 private JScrollPane scroll; | 47 private JScrollPane scroll; |
48 | 48 |
49 private JMenuItem menu_calendar; | 49 private JMenuItem menu_calendar; |
50 private JMenuItem menu_resaveAll; | 50 private JMenuItem menu_resaveAll; |
51 | 51 |
52 public TabSimpleSearch() | 52 public TabSimpleSearch() |
53 { | 53 { |
54 dirty = true; | 54 dirty = true; |
55 | 55 |
56 this.setLayout( new BorderLayout() ); | 56 this.setLayout( new BorderLayout() ); |
57 | 57 |
58 JPanel top = new JPanel( new BorderLayout() ); | 58 JPanel top = new JPanel( new BorderLayout() ); |
59 Box buttonBox = Box.createHorizontalBox(); | 59 Box buttonBox = Box.createHorizontalBox(); |
60 context = new JButton("="); | 60 context = new JButton("="); |
61 create = new JButton("+"); | 61 create = new JButton("+"); |
62 field = new JTextField(); | 62 field = new JTextField(); |
63 results = Box.createVerticalBox(); | 63 results = Box.createVerticalBox(); |
64 scroll = new JScrollPane( results ); | 64 scroll = new JScrollPane( results ); |
65 | 65 |
66 menu = new JPopupMenu("Menu"); | 66 menu = new JPopupMenu("Menu"); |
67 menu_calendar = menu.add("Calendar board"); | 67 menu_calendar = menu.add("Calendar board"); |
68 menu_resaveAll = menu.add("Resave all cards (developer)"); | 68 menu_resaveAll = menu.add("Resave all cards (developer)"); |
69 | 69 |
70 context.setFont( new Font( "Monospaced", Font.BOLD, 16 ) ); | 70 context.setFont( new Font( "Monospaced", Font.BOLD, 16 ) ); |
71 create.setFont( new Font( "Monospaced", Font.BOLD, 16 ) ); | 71 create.setFont( new Font( "Monospaced", Font.BOLD, 16 ) ); |
72 field.setFont( new Font( "Monospaced", Font.PLAIN, 16 ) ); | 72 field.setFont( new Font( "Monospaced", Font.PLAIN, 16 ) ); |
73 | 73 |
74 scroll.getVerticalScrollBar().setUnitIncrement(128); | 74 scroll.getVerticalScrollBar().setUnitIncrement(128); |
75 | 75 |
76 field.setPreferredSize( new Dimension(32, 32) ); | 76 field.setPreferredSize( new Dimension(32, 32) ); |
77 | 77 |
78 this.add( top, BorderLayout.NORTH ); | 78 this.add( top, BorderLayout.NORTH ); |
79 top.add( field, BorderLayout.CENTER ); | 79 top.add( field, BorderLayout.CENTER ); |
80 top.add( buttonBox, BorderLayout.EAST ); | 80 top.add( buttonBox, BorderLayout.EAST ); |
81 buttonBox.add( context ); | 81 buttonBox.add( context ); |
82 buttonBox.add( create ); | 82 buttonBox.add( create ); |
83 | 83 |
84 this.add( scroll, BorderLayout.CENTER ); | 84 this.add( scroll, BorderLayout.CENTER ); |
85 | 85 |
86 scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); | 86 scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); |
87 | 87 |
88 create.addActionListener(this); | 88 create.addActionListener(this); |
89 context.addActionListener(this); | 89 context.addActionListener(this); |
90 menu_calendar.addActionListener(this); | 90 menu_calendar.addActionListener(this); |
91 menu_resaveAll.addActionListener(this); | 91 menu_resaveAll.addActionListener(this); |
92 | 92 |
93 field.getDocument().addDocumentListener( | 93 field.getDocument().addDocumentListener( |
94 new DocumentListener() | 94 new DocumentListener() |
95 { | 95 { |
96 @Override | 96 @Override |
97 public void changedUpdate( DocumentEvent e ) | 97 public void changedUpdate( DocumentEvent e ) |
98 { | 98 { |
99 updateTitle(); | 99 updateTitle(); |
100 search(); | 100 search(); |
101 } | 101 } |
102 @Override | 102 @Override |
103 public void removeUpdate( DocumentEvent e ) | 103 public void removeUpdate( DocumentEvent e ) |
104 { | 104 { |
105 updateTitle(); | 105 updateTitle(); |
106 search(); | 106 search(); |
107 } | 107 } |
108 @Override | 108 @Override |
109 public void insertUpdate( DocumentEvent e ) | 109 public void insertUpdate( DocumentEvent e ) |
110 { | 110 { |
111 updateTitle(); | 111 updateTitle(); |
112 search(); | 112 search(); |
113 } | 113 } |
114 } | 114 } |
115 ); | 115 ); |
116 | 116 |
117 registerKeyboardAction( this, KEY_ACTION_COMMIT, KeyStroke.getKeyStroke( KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK ), WHEN_IN_FOCUSED_WINDOW ); | 117 registerKeyboardAction( this, KEY_ACTION_COMMIT, KeyStroke.getKeyStroke( KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK ), WHEN_IN_FOCUSED_WINDOW ); |
118 | 118 |
119 field.setToolTipText("Search query."); | 119 field.setToolTipText("Search query."); |
120 create.setToolTipText("Create new card. Shift-click to open it in a new window."); | 120 create.setToolTipText("Create new card. Shift-click to open it in a new window."); |
121 context.setToolTipText("Bring up a menu with more actions."); | 121 context.setToolTipText("Bring up a menu with more actions."); |
122 menu_calendar.setToolTipText("Open calendar board."); | 122 menu_calendar.setToolTipText("Open calendar board."); |
123 menu_resaveAll.setToolTipText("Resave all cards. Might be useful if you updated program version and database format changed."); | 123 menu_resaveAll.setToolTipText("Resave all cards. Might be useful if you updated program version and database format changed."); |
124 | 124 |
125 } | 125 } |
126 | 126 |
127 private void search() { | 127 private void search() { |
128 | 128 |
129 if( !javax.swing.SwingUtilities.isEventDispatchThread() ) { | 129 /* TODO: Is this needed? */ |
130 SwingUtilities.invokeLater( | 130 if( !javax.swing.SwingUtilities.isEventDispatchThread() ) { |
131 new Runnable() | 131 SwingUtilities.invokeLater( |
132 { | 132 new Runnable() |
133 public void run() | 133 { |
134 { | 134 public void run() |
135 search(); | 135 { |
136 } | 136 search(); |
137 } | 137 } |
138 ); | 138 } |
139 return; | 139 ); |
140 } | 140 return; |
141 | 141 } |
142 Card[] cards; | 142 |
143 | 143 Card[] cards; |
144 String text = field.getText(); | 144 |
145 if( text.length() > 0 ) { | 145 String text = field.getText(); |
146 cards = Main.database.searchSimple( field.getText() ); | 146 if( text.length() > 0 ) { |
147 } else { | 147 cards = Main.database.searchSimple( field.getText() ); |
148 cards = Main.database.searchTopRecent( 32 ); | 148 } else { |
149 } | 149 cards = Main.database.searchTopRecent( 32 ); |
150 | 150 } |
151 System.out.print("Search: Found "+cards.length+" matches.\n"); | 151 |
152 | 152 System.out.print("Search: Found "+cards.length+" matches.\n"); |
153 /* TODO: Reuse widgets. */ | 153 |
154 results.removeAll(); | 154 /* TODO: Reuse widgets. */ |
155 for( Card card : cards ) { | 155 results.removeAll(); |
156 CardWidget cardWidget = new CardWidget( card ); | 156 for( Card card : cards ) { |
157 results.add( cardWidget ); | 157 CardWidget cardWidget = new CardWidget( card ); |
158 } | 158 results.add( cardWidget ); |
159 results.validate(); | 159 } |
160 results.repaint(); | 160 results.validate(); |
161 | 161 results.repaint(); |
162 /* Otherwise scrollup doesn't work. Perhaps because GUI needs to redraw first? */ | 162 |
163 SwingUtilities.invokeLater( | 163 /* Otherwise scrollup doesn't work. Perhaps because GUI needs to redraw first? */ |
164 new Runnable() | 164 SwingUtilities.invokeLater( |
165 { | 165 new Runnable() |
166 public void run() | 166 { |
167 { | 167 public void run() |
168 scrollTop(); | 168 { |
169 } | 169 scrollTop(); |
170 } | 170 } |
171 ); | 171 } |
172 } | 172 ); |
173 | 173 } |
174 public void refreshSearch() | 174 |
175 { | 175 public void refreshSearch() |
176 search(); | 176 { |
177 dirty = false; | 177 search(); |
178 } | 178 dirty = false; |
179 | 179 } |
180 private void scrollTop() | 180 |
181 { | 181 private void scrollTop() |
182 JScrollBar scrollbar = scroll.getVerticalScrollBar(); | 182 { |
183 scrollbar.setValue(0); | 183 JScrollBar scrollbar = scroll.getVerticalScrollBar(); |
184 } | 184 scrollbar.setValue(0); |
185 | 185 } |
186 private void updateTitle() | 186 |
187 { | 187 private void updateTitle() |
188 Window window = (Window)this.getTopLevelAncestor(); | 188 { |
189 | 189 Window window = (Window)this.getTopLevelAncestor(); |
190 String text = field.getText(); | 190 |
191 | 191 String text = field.getText(); |
192 if( text.length() > 0 ) { | 192 |
193 window.setTitle( window.preferredTitle( "Search: "+text ) ); | 193 if( text.length() > 0 ) { |
194 } else { | 194 window.setTitle( window.preferredTitle( "Search: "+text ) ); |
195 window.setTitle( window.preferredTitle( "Search" ) ); | 195 } else { |
196 } | 196 window.setTitle( window.preferredTitle( "Search" ) ); |
197 | 197 } |
198 } | 198 |
199 | 199 } |
200 private void buttonClickedCreate( boolean newWindow ) | 200 |
201 { | 201 private void buttonClickedCreate( boolean newWindow ) |
202 if( newWindow ) { | 202 { |
203 Main.actionCardCreate( Main.windowAdd( Tab.EDIT ) ); | 203 if( newWindow ) { |
204 } else { | 204 Main.actionCardCreate( Main.windowAdd( Tab.EDIT ) ); |
205 Window window = (Window)this.getTopLevelAncestor(); | 205 } else { |
206 Main.actionCardCreate( window ); | 206 Window window = (Window)this.getTopLevelAncestor(); |
207 } | 207 Main.actionCardCreate( window ); |
208 } | 208 } |
209 | 209 } |
210 public void actionPerformed( ActionEvent e ) | 210 |
211 { | 211 public void actionPerformed( ActionEvent e ) |
212 Object source = e.getSource(); | 212 { |
213 if( source == this ) { | 213 Object source = e.getSource(); |
214 switch( e.getActionCommand() ){ | 214 if( source == this ) { |
215 | 215 switch( e.getActionCommand() ){ |
216 case KEY_ACTION_COMMIT: { | 216 |
217 Main.database.databaseCommit(); | 217 case KEY_ACTION_COMMIT: { |
218 break; | 218 Main.database.databaseCommit(); |
219 } | 219 break; |
220 | 220 } |
221 } | 221 |
222 } else if( source == create ) { | 222 } |
223 boolean newWindow = (e.getModifiers() & ActionEvent.SHIFT_MASK) != 0; | 223 } else if( source == create ) { |
224 buttonClickedCreate( newWindow ); | 224 boolean newWindow = (e.getModifiers() & ActionEvent.SHIFT_MASK) != 0; |
225 } else if( source == context ) { | 225 buttonClickedCreate( newWindow ); |
226 menu.show( (Component)source, 0, 0 ); | 226 } else if( source == context ) { |
227 } else if( source == menu_calendar ) { | 227 menu.show( (Component)source, 0, 0 ); |
228 Window window = (Window)this.getTopLevelAncestor(); | 228 } else if( source == menu_calendar ) { |
229 window.tabSwitch( Tab.CALENDAR_BOARD ); | 229 Window window = (Window)this.getTopLevelAncestor(); |
230 } else if( source == menu_resaveAll ) { | 230 window.tabSwitch( Tab.CALENDAR_BOARD ); |
231 Main.database.databaseResaveAll(); | 231 } else if( source == menu_resaveAll ) { |
232 Main.refreshSearches(); | 232 Main.database.databaseResaveAll(); |
233 } | 233 Main.refreshSearches(); |
234 } | 234 } |
235 | 235 } |
236 public void onSwitchedTo() { | 236 |
237 if( dirty ) { | 237 public void onSwitchedTo() { |
238 refreshSearch(); | 238 if( dirty ) { |
239 dirty = false; | 239 refreshSearch(); |
240 } | 240 dirty = false; |
241 } | 241 } |
242 | 242 } |
243 | |
243 } | 244 } |