Developer Tools
JDeveloper
1
package
shay
.
org
.
ext
;
2
/*
3
* This extension adds a menu option to the Search menu
4
* The option "Go To File" provides a list of the files in the currently
5
* active project and its sub directories.
6
* Choosing a file will open it in the JDeveloper editor.
7
*
8
* Created by Shay Shmeltzer
9
* July 2007
10
*/
11
12
import
java
.
io
.
File
;
13
14
import
java
.
net
.
MalformedURLException
;
15
import
java
.
net
.
URL
;
16
17
import
java
.
util
.
ArrayList
;
18
import
java
.
util
.
Collections
;
19
import
java
.
util
.
List
;
20
21
import
java
.
util
.
Vector
;
22
23
import
javax
.
swing
.
Icon
;
24
import
javax
.
swing
.
JComboBox
;
25
import
javax
.
swing
.
JComponent
;
26
import
javax
.
swing
.
JMenu
;
27
import
javax
.
swing
.
JMenuItem
;
28
import
javax
.
swing
.
JTextField
;
29
30
import
oracle
.
bali
.
ewt
.
dialog
.
JEWTDialog
;
31
32
import
oracle
.
ide
.
Addin
;
33
import
oracle
.
ide
.
Context
;
34
import
oracle
.
ide
.
Ide
;
35
import
oracle
.
ide
.
controller
.
Controller
;
36
import
oracle
.
ide
.
controller
.
IdeAction
;
37
import
oracle
.
ide
.
controller
.
Menubar
;
38
import
oracle
.
ide
.
dialogs
.
OnePageWizardDialogFactory
;
39
import
oracle
.
ide
.
dialogs
.
WizardLauncher
;
40
import
oracle
.
ide
.
editor
.
EditorManager
;
41
import
oracle
.
ide
.
help
.
HelpSystem
;
42
import
oracle
.
ide
.
keyboard
.
KeyStrokeContextRegistry
;
43
import
oracle
.
ide
.
log
.
LogManager
;
44
import
oracle
.
ide
.
model
.
Project
;
45
46
public
final
class
FileBrowser
implements
Addin
,
Controller
{
47
// Simple Menu item
48
public
static
final
int
GO_TO_FILE_CMD_ID
=
Ide
.
findOrCreateCmdID
(
"GO_TO_FILE_CMD"
)
;
49
public
List
<
String
>
projectFiles
;
50
51
public
boolean
handleEvent
(
IdeAction
action
,
Context
context
)
{
52
int
cmdId
=
action
.
getCommandId
()
;
53
String
msg
=
null
;
54
//populate a list of the files in the active project
55
projectFiles
=
56
populateFileListForProject
(
Ide
.
getActiveProject
()
.
getBaseDirectory
())
;
57
58
//create and open the dialog
59
if
(
cmdId
==
GO_TO_FILE_CMD_ID
)
{
60
Collections
.
sort
(
projectFiles
)
;
61
JComboBox
cb
=
new
JComboBox
(
new
Vector
(
projectFiles
))
;
62
JEWTDialog
dlg
=
63
OnePageWizardDialogFactory
.
createJEWTDialog
(
cb
,
null
,
64
"Enter File Name"
)
;
65
dlg
.
setDefaultButton
(
JEWTDialog
.
BUTTON_OK
)
;
66
dlg
.
setOKButtonEnabled
(
true
)
;
67
68
boolean
go
=
WizardLauncher
.
runDialog
(
dlg
)
;
69
if
(
go
)
{
70
try
{
71
//open the specified file
72
EditorManager
.
getEditorManager
()
.
openDefaultEditorInFrame
(
new
URL
(
"file:///"
+
73
cb
.
getSelectedItem
()
.
toString
()
.
substring
(
cb
.
getSelectedItem
()
.
toString
()
.
indexOf
(
" # "
)
+
74
3
)))
;
75
}
catch
(
MalformedURLException
e
)
{
76
System
.
out
.
println
(
e
)
;
77
}
78
}
79
}
80
81
if
(
msg
!=
null
)
{
82
logMessage
(
msg
)
;
83
Ide
.
getStatusBar
()
.
setText
(
msg
)
;
84
return
true
;
85
}
86
return
false
;
87
}
88
89
private
static
final
void
logMessage
(
String
msg
)
{
90
LogManager
.
getLogManager
()
.
showLog
()
;
91
LogManager
.
getLogManager
()
.
getMsgPage
()
.
log
(
msg
+
"\n"
)
;
92
}
93
94
static
boolean
enableElementInfo
(
Context
context
)
{
95
return
true
;
96
}
97
98
public
boolean
update
(
IdeAction
action
,
Context
context
)
{
99
int
cmdId
=
action
.
getCommandId
()
;
100
101
if
(
cmdId
==
GO_TO_FILE_CMD_ID
)
{
102
action
.
setEnabled
(
enableElementInfo
(
context
))
;
103
return
true
;
104
}
105
return
false
;
106
}
107
108
public
void
initialize
()
{
109
// Create the Context Menu Item and add it to the Navigator Menu
110
Controller
ctrlr1
=
this
;
111
JMenuItem
mi
=
112
doCreateMenuItem
(
ctrlr1
,
"Go To File ..."
,
new
Integer
(
'O'
))
;
113
JMenu
jmenu
=
Menubar
.
getJMenu
(
"Search"
)
;
114
jmenu
.
add
(
mi
)
;
115
116
//This section adds the file that specify the keyboard accelerator
117
KeyStrokeContextRegistry
r
=
Ide
.
getKeyStrokeContextRegistry
()
;
118
r
.
addAcceleratorDefinitionFile
(
FileBrowser
.
class
.
getClassLoader
()
,
119
"META-INF/accelerators.xml"
)
;
120
}
121
122
private
static
JMenuItem
doCreateMenuItem
(
Controller
ctrlr
,
123
String
menuLabel
,
124
Integer
mnemonic
)
{
125
// Icon associated with this IdeAction
126
Icon
icon
=
null
;
127
// Category (if any) associated with this IdeAction
128
String
category
=
"Search"
;
129
// Name of class which extends oracle.ide.addin.AbstractCommand
130
String
cmdClass
=
null
;
131
132
// This can be retrieved via IdeAction.find(MY_MENU_COMMAND_ID)
133
IdeAction
actionTM
=
// int cmdId,
134
// extends oracle.ide.addin.AbstractCommand
135
// String name,
136
// Category
137
// Integer mnemonic,
138
// Icon icon,
139
// Object data,
140
// boolean enabled
141
IdeAction
.
get
(
GO_TO_FILE_CMD_ID
,
cmdClass
,
menuLabel
,
category
,
142
mnemonic
,
icon
,
null
,
true
)
;
143
144
actionTM
.
addController
(
ctrlr
)
;
145
JMenuItem
menuItem
=
Ide
.
getMenubar
()
.
createMenuItem
(
actionTM
)
;
146
return
menuItem
;
147
}
148
149
150
private
List
populateFileListForProject
(
String
p_dir
)
{
151
File
dir
=
new
File
(
p_dir
)
;
152
List
justFiles
=
new
ArrayList
()
;
153
int
filesCounter
=
0
;
154
155
String
[]
children
=
dir
.
list
()
;
156
if
(
children
==
null
)
{
157
// Either dir does not exist or is not a directory
158
}
else
{
159
160
for
(
int
i
=
0
;
i
<
children
.
length
;
i
++
)
{
161
File
as
=
new
File
(
p_dir
+
"/"
+
children
[
i
])
;
162
if
(
!
as
.
isDirectory
())
{
163
justFiles
.
add
(
children
[
i
]
.
toLowerCase
()
+
" # "
+
164
as
.
getAbsolutePath
())
;
165
filesCounter
++;
166
}
else
{
167
List
sub
=
168
populateFileListForProject
(
as
.
getAbsolutePath
())
;
169
for
(
int
k
=
0
;
k
<
sub
.
size
()
;
k
++
)
{
170
justFiles
.
add
(
sub
.
get
(
k
))
;
171
filesCounter
++;
172
}
173
174
}
175
}
176
177
for
(
int
j
=
0
;
j
<
justFiles
.
size
()
;
j
++
)
{
178
System
.
out
.
println
(
justFiles
.
get
(
j
))
;
179
}
180
181
}
182
183
return
justFiles
;
184
}
185
186
187
}
188