-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickOpenForm.java
More file actions
460 lines (344 loc) · 13.8 KB
/
Copy pathQuickOpenForm.java
File metadata and controls
460 lines (344 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
import com.intellij.ide.DataManager;
import com.intellij.ide.actions.GotoLineAction;
import com.intellij.ide.actions.OpenFileAction;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.*;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import javax.swing.*;
import javax.swing.JTextField ;
import javax.swing.JTextField ;
import javax.swing.JTextField ;
import javax.swing.JPanel ;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
public class QuickOpenForm extends JFrame {
private static final long serialVersionUID = 1L;
// component definition
// filter info input
private JLabel infoLabel;
private JTextField hierarchyText1;
private JTextField hierarchyText2;
private JTextField hierarchyText3;
private JTextField hierarchyText4;
private JTextField detailText1;
private JTextField detailText2;
private JButton confirmBtn;
private JPanel panel1;
// text to filter data
String hierarchyInfo1 = "" ;
String hierarchyInfo2 = "" ;
String hierarchyInfo3 = "";
String hierarchyInfo4 = "";
String detailInfo1 = "";
String detailInfo2 = "";
// text to find target line
private JTextField targetText;
private JButton tarConfirmBtn;
private JPanel tarPanel;
private String targetInfo = "";
// JTable to display data
private JTable pageInfoTable;
private JPanel panel2;
// store display data
ArrayList<String> descList = new ArrayList<String>();
ArrayList<String> jspNameList = new ArrayList<String>();
ArrayList<String> flowNameList = new ArrayList<String>();
ArrayList<String> pageList = new ArrayList(); // origin data of file shortcut
Project project; // idea project
String filePrefix = ""; // file prefix path
// filter the data according to input text
private void filterTableData() {
descList.clear();
flowNameList.clear();
jspNameList.clear();
// filter the data according to input text
for( String item : pageList ) {
String [] lineSplit = item.split(":");
String description = lineSplit[0].trim();
// display the data if it contains the specific text
String [] descriptions = description.split(",");
String hierarchyDesc1 = descriptions[0].trim() ;
String hierarchyDesc2 = descriptions[1].trim() ;
String hierarchyDesc3 = descriptions[2].trim() ;
String hierarchyDesc4 = descriptions[3].trim() ;
String detailDesc1 = descriptions[4].trim() ;
String detailDesc2 = descriptions[5].trim() ;
boolean flag1 = hierarchyInfo1.isEmpty() || (!hierarchyDesc1.isEmpty() && hierarchyDesc1.equals(hierarchyInfo1));
boolean flag2 = hierarchyInfo2.isEmpty() || (!hierarchyDesc2.isEmpty() && hierarchyDesc2.equals(hierarchyInfo2));
boolean flag3 = hierarchyInfo3.isEmpty() || (!hierarchyDesc3.isEmpty() && hierarchyDesc3.equals(hierarchyInfo3));
boolean flag4 = hierarchyInfo4.isEmpty() || (!hierarchyDesc4.isEmpty() && hierarchyDesc4.equals(hierarchyInfo4));
boolean flag5 = detailInfo1.isEmpty() || (!detailDesc1.isEmpty() && detailDesc1.equals(detailInfo1));
boolean flag6 = detailInfo2.isEmpty() || (!detailDesc2.isEmpty() && detailDesc2.equals(detailInfo2));
// if it satisfies all condition
if( flag1 && flag2 && flag3 && flag4 && flag5 && flag6 ) {
descList.add(description);
String fileInfo = lineSplit[1].trim();
String [] fileInfos = fileInfo.split(";");
String pageFlowName = fileInfos[0].trim();
String jspFileName = fileInfos[1].trim();
flowNameList.add(pageFlowName);
jspNameList.add(jspFileName);
}
}
}
// set data in the JTable
public void setTableData() {
this.filterTableData(); // filter the data
DefaultTableModel newtablemodel = new DefaultTableModel() {
private static final long serialVersionUID = 1L;
// set non editable
public boolean isCellEditable(int rowIndex, int ColIndex) {
return false;
}
};
Vector v = new Vector(descList);
newtablemodel.addColumn("Description", v);
v = new Vector(flowNameList);
newtablemodel.addColumn("Realted File1", v);
v = new Vector(jspNameList);
newtablemodel.addColumn("Realted File2", v);
pageInfoTable.setModel(newtablemodel); // add data
// refresh the JTable
SwingUtilities.invokeLater(new Runnable() {
public void run() {
pageInfoTable.updateUI();
}
});
}
// set component size
public void setSize() {
// text to filter data
infoLabel.setPreferredSize(new Dimension(80,40));
hierarchyText1.setPreferredSize(new Dimension(100,40));
hierarchyText2.setPreferredSize(new Dimension(100,40));
hierarchyText3.setPreferredSize(new Dimension(100,40));
hierarchyText4.setPreferredSize(new Dimension(100,40));
detailText1.setPreferredSize(new Dimension(100,40));
detailText2.setPreferredSize(new Dimension(100,40));
confirmBtn.setPreferredSize(new Dimension(80,40));
}
// set tarString component size
public void setTargetSize() {
// text to find target line
targetText.setPreferredSize(new Dimension(400,40));
tarConfirmBtn.setPreferredSize(new Dimension(120,40));
}
// init the JFrame
public QuickOpenForm(ArrayList<String> pageList, Project p, String filePrefix) {
this.pageList = pageList;
this.project = p;
this.filePrefix = filePrefix;
// init the component
panel1 = new JPanel();
infoLabel = new JLabel("filter info");
hierarchyText1 = new JTextField();
hierarchyText2 = new JTextField();
hierarchyText3 = new JTextField();
hierarchyText4 = new JTextField();
detailText1 = new JTextField();
detailText2 = new JTextField();
confirmBtn = new JButton("confirm");
// put into panel
panel1.add(infoLabel);
panel1.add(hierarchyText1);
panel1.add(hierarchyText2);
panel1.add(hierarchyText3);
panel1.add(hierarchyText4);
panel1.add(detailText1);
panel1.add(detailText2);
panel1.add(confirmBtn);
this.addConfirmListener(); // add listener to confirmBtn
this.setSize();
// text to find target line
targetText = new JTextField();
tarConfirmBtn = new JButton("tar confirm");
tarPanel = new JPanel();
this.setTargetSize();
this.addtarConfirmListener();
// add into panel
tarPanel.add(targetText);
tarPanel.add(tarConfirmBtn);
panel2 = new JPanel();
pageInfoTable = new JTable() {
private static final long serialVersionUID = 1L;
// set non editable
public boolean isCellEditable(int rowIndex, int ColIndex) {
return false;
}
};
this.addActionListenForTable(); // add click listener
// set text font
pageInfoTable.getTableHeader().setFont(new Font("Serif", Font.PLAIN, 18));
pageInfoTable.setFont(new Font("Serif", Font.PLAIN, 15));
// set scroll bar
JScrollPane sp =new JScrollPane(pageInfoTable);
sp.setPreferredSize( new Dimension(700,500 ));
// put into panel
panel2.setLayout(new BorderLayout());
panel2.add(sp,BorderLayout.CENTER );
panel2.add(sp);
// add panel into JFrame
this.add(panel1, BorderLayout.NORTH);
this.add(tarPanel, BorderLayout.CENTER);
this.add(panel2, BorderLayout.SOUTH);
this.pack();
// set size
this.setSize(900, 650);
this.setLocation(200, 200);
this.setVisible(true);
// set close event
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// pageInfoTable.setGridColor(java.awt.Color.BLUE);
}
// add table data cell click listener
private void addActionListenForTable() {
pageInfoTable.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
// double click to trigger
if (e.getClickCount() == 2) {
// get the cell which are being clicked
int row = pageInfoTable.getSelectedRow();
int col = pageInfoTable.getSelectedColumn();
String filePath = (String)pageInfoTable.getValueAt(row, col);
// System.out.println( "open file : " + row + " " + col + " " + filePath);
// open file and go to the line num
String fileName = filePrefix + "/" + filePath;
OpenFileAction.openFile( fileName, project);
// if target string is not empty
if(targetInfo!= null && !targetInfo.trim().isEmpty()) {
int lineNum = findLineNum(fileName, targetInfo);
if(lineNum >= 0) {
gotoLine(lineNum);
}
}
}
}
});
}
// find the line number of the target String
private int findLineNum(String fileName, String target) {
int lineNum = 0;
File file = new File(fileName);
BufferedReader reader = null;
boolean isFind = false;
try {
reader = new BufferedReader(new FileReader(file));
String line = null;
// read oneline per iteration
while ((line = reader.readLine()) != null) {
lineNum ++;
// find the target String
if( line.trim().equals(target.trim())) {
isFind = true;
break;
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
// if find the target String
if(isFind && lineNum > 0){
return lineNum;
}
return -1;
}
// add confirmBtn click listener
private void addConfirmListener(){
this.confirmBtn.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
hierarchyInfo1 = hierarchyText1.getText().trim();
hierarchyInfo2 = hierarchyText2.getText().trim();
hierarchyInfo3 = hierarchyText3.getText().trim();
hierarchyInfo4 = hierarchyText4.getText().trim();
detailInfo1 = detailText1.getText().trim();
detailInfo2 = detailText2.getText().trim();
setTableData(); // update the data JTable display
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
}
// add tarConfirmBtn click listener
private void addtarConfirmListener(){
this.tarConfirmBtn.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
targetInfo = targetText.getText().trim();
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
}
public boolean gotoLine(int lineNumber ) {
DataContext dataContext = DataManager.getInstance().getDataContext();
// Project project = getProject(dataContext);
Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
if( editor == null )
return false;
CaretModel caretModel = editor.getCaretModel();
int totalLineCount = editor.getDocument().getLineCount();
if( lineNumber > totalLineCount )
return false;
//Moving caret to line number
int realNum = lineNumber>1? lineNumber-1 : 0;
caretModel.moveToLogicalPosition(new LogicalPosition(realNum,0));
//Scroll to the caret
ScrollingModel scrollingModel = editor.getScrollingModel();
scrollingModel.scrollToCaret(ScrollType.CENTER);
return true;
}
}