-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFindUI.rb
172 lines (145 loc) · 4.9 KB
/
FindUI.rb
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
module Yoshied
class FindUI
LEADING = GroupLayout::Alignment::LEADING
def initialize(app)
@app = app
@dialog = nil
@textField = nil
@caseCheckbox = nil
end
def create(owner)
@dialog = JDialog.new(owner, "Find")
@dialog.setLayout(BorderLayout.new)
@dialog.add(createContentPane, BorderLayout::CENTER)
addBindings(@dialog.getRootPane)
@dialog.pack
locateDialog
end
def createContentPane
textPane = createTextPane
buttonPane = createButtonPane
panel = JPanel.new
layout = GroupLayout.new(panel)
panel.setLayout(layout)
group = layout.createSequentialGroup
group.addComponent(textPane).
addComponent(buttonPane)
layout.setHorizontalGroup(group)
group = layout.createSequentialGroup
group.addGroup(layout.createParallelGroup.
addComponent(textPane).
addComponent(buttonPane))
layout.setVerticalGroup(group)
return panel
end
def createTextPane
@textField = createTextField
@caseCheckbox = JCheckBox.new("Match Case")
spacer1 = javax.swing.JLabel.new(" ")
spacer2 = javax.swing.JLabel.new(" ")
panel = JPanel.new
layout = GroupLayout.new(panel)
panel.setLayout(layout)
layout.setAutoCreateGaps(true)
layout.setAutoCreateContainerGaps(true)
group = layout.createSequentialGroup
group.addGroup(layout.createParallelGroup(LEADING).
addComponent(@textField).
addComponent(@caseCheckbox).
addComponent(spacer1).
addComponent(spacer2))
layout.setHorizontalGroup(group)
group = layout.createSequentialGroup
group.addComponent(@textField).
addComponent(@caseCheckbox).
addComponent(spacer1).
addComponent(spacer2)
layout.setVerticalGroup(group)
return panel
end
def createTextField
textField = javax.swing.JTextField.new(30)
textField.setBorder(BorderFactory.createEtchedBorder)
return textField
end
def createButtonPane
prevButton = createPrevButton
nextButton = createNextButton
@dialog.rootPane.setDefaultButton(nextButton)
cancelButton = createCancelButton
panel = JPanel.new
layout = GroupLayout.new(panel)
panel.setLayout(layout)
layout.setAutoCreateGaps(true)
layout.setAutoCreateContainerGaps(true)
group = layout.createSequentialGroup
group.addGroup(layout.createParallelGroup(LEADING).
addComponent(prevButton).
addComponent(nextButton).
addComponent(cancelButton))
layout.setHorizontalGroup(group)
sym = "java.awt.Component".to_sym
layout.linkSize(SwingConstants.HORIZONTAL,
[prevButton, nextButton, cancelButton].to_java(sym))
group = layout.createSequentialGroup
group.addComponent(prevButton).
addComponent(nextButton).
addComponent(cancelButton)
layout.setVerticalGroup(group)
return panel
end
def createPrevButton
button = JButton.new("Find Prev")
button.setMnemonic(KeyEvent::VK_P)
button.addActionListener{onFindPrev}
return button
end
def createNextButton
button = JButton.new("Find Next")
button.setMnemonic(KeyEvent::VK_N)
button.addActionListener{onFindNext}
return button
end
def createCancelButton
button = JButton.new("Cancel")
button.addActionListener{onCancel}
return button
end
def addBindings(rootPane)
cancelAction = ProcTextAction.new("cancel"){onCancel}
rootPane.getInputMap(JComponent::WHEN_IN_FOCUSED_WINDOW).
put(KeyStroke.getKeyStroke(KeyEvent::VK_ESCAPE, 0), "cancelAction")
rootPane.getActionMap.put("cancelAction", cancelAction)
end
def locateDialog
@dialog.setLocation(calcLocation)
end
def calcLocation
ownerRect = @app.frame.getBounds
dim = @dialog.getSize
x = ownerRect.x + (ownerRect.width - dim.width) / 2
y = ownerRect.y + (ownerRect.height - dim.height) / 2
return java.awt.Point.new(x, y)
end
def onFindPrev
!@textField and return
str = @textField.getText
str.empty? and return
@app.findPrev(str, @caseCheckbox.isSelected)
end
def onFindNext
!@textField and return
str = @textField.getText
str.empty? and return
@app.findNext(str, @caseCheckbox.isSelected)
end
def onCancel
@dialog.setVisible(false)
end
def show
!@dialog and create(@app.frame)
@dialog.setVisible(true)
@textField.requestFocusInWindow
end
end
end