| 1 |
/* |
| 2 |
* Copyright (c) 1997, 2019 Oracle and/or its affiliates. All rights reserved. |
| 3 |
* |
| 4 |
* This program and the accompanying materials are made available under the |
| 5 |
* terms of the Eclipse Distribution License v. 1.0, which is available at |
| 6 |
* http://www.eclipse.org/org/documents/edl-v10.php. |
| 7 |
* |
| 8 |
* SPDX-License-Identifier: BSD-3-Clause |
| 9 |
*/ |
| 10 |
|
| 11 |
import java.awt.*; |
| 12 |
import java.awt.event.*; |
| 13 |
import java.beans.*; |
| 14 |
import java.io.*; |
| 15 |
import jakarta.activation.*; |
| 16 |
|
| 17 |
public class JAFApp extends Frame implements WindowListener, ItemListener, ActionListener { |
| 18 |
|
| 19 |
// UI stuff... |
| 20 |
private GridBagLayout gridbag = null; |
| 21 |
private GridBagLayout panel_gb = null; |
| 22 |
private List file_list = null; |
| 23 |
private Panel results_panel = null; |
| 24 |
private Label f_title = null; |
| 25 |
private Label f_name_label = null; |
| 26 |
private Label type_label = null; |
| 27 |
private Label type_title = null; |
| 28 |
private Button launch_button = null; |
| 29 |
private Choice verb_choice = null; |
| 30 |
// File stuff... |
| 31 |
private File _file = null; |
| 32 |
// private FileDataSource fds[] = null; |
| 33 |
private DataHandler dh[] = null; |
| 34 |
|
| 35 |
private boolean debug = false; |
| 36 |
// private |
| 37 |
/////////////////////////////////////////////////////////////////////// |
| 38 |
// ctor |
| 39 |
public JAFApp(String title) |
| 40 |
{ |
| 41 |
super(title); |
| 42 |
initMe(); |
| 43 |
} |
| 44 |
|
| 45 |
public JAFApp() |
| 46 |
{ |
| 47 |
super(); |
| 48 |
initMe(); |
| 49 |
} |
| 50 |
|
| 51 |
/////////////////////////////////////////////////////////////////////// |
| 52 |
// initME -- init the UI |
| 53 |
private void initMe() |
| 54 |
{ |
| 55 |
Font label_font = new Font("Default", Font.PLAIN, 12); |
| 56 |
Font title_font = new Font("Default", Font.BOLD, 12); |
| 57 |
|
| 58 |
gridbag = new GridBagLayout(); |
| 59 |
setLayout(gridbag); |
| 60 |
|
| 61 |
this.setBackground(Color.white); |
| 62 |
|
| 63 |
// results panel create |
| 64 |
results_panel = new Panel(); |
| 65 |
panel_gb = new GridBagLayout(); |
| 66 |
results_panel.setLayout(panel_gb); |
| 67 |
results_panel.setBackground(Color.lightGray); |
| 68 |
addGridComponent(this, |
| 69 |
results_panel, |
| 70 |
gridbag, |
| 71 |
0,0, |
| 72 |
1,1, |
| 73 |
1,0 ); |
| 74 |
|
| 75 |
// file name label |
| 76 |
f_title = new Label("File Name:"); |
| 77 |
f_title.setBackground(Color.lightGray); |
| 78 |
f_title.setAlignment(Label.RIGHT); |
| 79 |
f_title.setFont(title_font); |
| 80 |
addGridComponent(results_panel, |
| 81 |
f_title, |
| 82 |
panel_gb, |
| 83 |
0, 0, |
| 84 |
1, 1, |
| 85 |
5, 100); |
| 86 |
|
| 87 |
// file name label |
| 88 |
f_name_label = new Label("<none selected>"); |
| 89 |
f_name_label.setBackground(Color.lightGray); |
| 90 |
f_name_label.setAlignment(Label.LEFT); |
| 91 |
f_name_label.setFont(label_font); |
| 92 |
addGridComponent(results_panel, |
| 93 |
f_name_label, |
| 94 |
panel_gb, |
| 95 |
1, 0, |
| 96 |
1, 1, |
| 97 |
15, 100); |
| 98 |
|
| 99 |
// file name label |
| 100 |
f_title = new Label("MIME Type:"); |
| 101 |
f_title.setBackground(Color.lightGray); |
| 102 |
f_title.setAlignment(Label.RIGHT); |
| 103 |
f_title.setFont(title_font); |
| 104 |
addGridComponent(results_panel, |
| 105 |
f_title, |
| 106 |
panel_gb, |
| 107 |
2, 0, |
| 108 |
1, 1, |
| 109 |
5, 100); |
| 110 |
|
| 111 |
// type_label |
| 112 |
type_label = new Label("<no type>"); |
| 113 |
type_label.setBackground(Color.lightGray); |
| 114 |
type_label.setAlignment(Label.LEFT); |
| 115 |
type_label.setFont(label_font); |
| 116 |
addGridComponent(results_panel, |
| 117 |
type_label, |
| 118 |
panel_gb, |
| 119 |
3,0, |
| 120 |
1,1, |
| 121 |
10, 100); |
| 122 |
|
| 123 |
// launch button |
| 124 |
launch_button = new Button("Launch!"); |
| 125 |
launch_button.setEnabled( false ); |
| 126 |
launch_button.setBackground(Color.red); |
| 127 |
launch_button.setFont(title_font); |
| 128 |
addGridComponent(results_panel, |
| 129 |
launch_button, |
| 130 |
panel_gb, |
| 131 |
4,0, |
| 132 |
1,1, |
| 133 |
5, 100); |
| 134 |
|
| 135 |
// verb popup |
| 136 |
verb_choice = new Choice(); |
| 137 |
verb_choice.setEnabled( false ); |
| 138 |
verb_choice.setFont(label_font); |
| 139 |
verb_choice.add("<empty>"); |
| 140 |
addGridComponent(results_panel, |
| 141 |
verb_choice, |
| 142 |
panel_gb, |
| 143 |
5,0, |
| 144 |
1,1, |
| 145 |
10, 100); |
| 146 |
|
| 147 |
// file list |
| 148 |
file_list = new List(); |
| 149 |
file_list.setBackground(Color.white); |
| 150 |
file_list.setFont(label_font); |
| 151 |
addGridComponent(this, |
| 152 |
file_list, |
| 153 |
gridbag, |
| 154 |
0,1, |
| 155 |
1, 2, |
| 156 |
1,1); |
| 157 |
|
| 158 |
this.invalidate(); |
| 159 |
|
| 160 |
// set up events |
| 161 |
this.addWindowListener(this); |
| 162 |
file_list.addItemListener(this); |
| 163 |
launch_button.addActionListener(this); |
| 164 |
|
| 165 |
} |
| 166 |
|
| 167 |
//////////////////////////////////////////////////////////////////////// |
| 168 |
/** |
| 169 |
* adds a component to our gridbag layout |
| 170 |
*/ |
| 171 |
private void addGridComponent(Container cont, |
| 172 |
Component comp, |
| 173 |
GridBagLayout mygb, |
| 174 |
int gridx, |
| 175 |
int gridy, |
| 176 |
int gridw, |
| 177 |
int gridh, |
| 178 |
int weightx, |
| 179 |
int weighty) |
| 180 |
{ |
| 181 |
GridBagConstraints c = new GridBagConstraints(); |
| 182 |
c.gridx = gridx; |
| 183 |
c.gridy = gridy; |
| 184 |
c.gridwidth = gridw; |
| 185 |
c.gridheight = gridh; |
| 186 |
c.fill = GridBagConstraints.BOTH; |
| 187 |
c.weighty = weighty; |
| 188 |
c.weightx = weightx; |
| 189 |
c.anchor = GridBagConstraints.CENTER; |
| 190 |
mygb.setConstraints(comp, c); |
| 191 |
cont.add(comp); |
| 192 |
} |
| 193 |
|
| 194 |
/////////////////////////////////////////////////////////////////////// |
| 195 |
// setFile - sets the file/directory for this frame at startup usually |
| 196 |
// |
| 197 |
public void setFile( File file ) |
| 198 |
{ |
| 199 |
_file = file; |
| 200 |
int i; |
| 201 |
MailcapCommandMap cmdmap = |
| 202 |
(MailcapCommandMap)CommandMap.getDefaultCommandMap(); |
| 203 |
cmdmap.addMailcap("application/x-int; jaf.viewers.IntViewer"); |
| 204 |
|
| 205 |
if(_file.isFile()) |
| 206 |
{ |
| 207 |
dh = new DataHandler[1]; |
| 208 |
dh[0] = new DataHandler(new FileDataSource( _file )); |
| 209 |
|
| 210 |
file_list.add( dh[0].getName() ); |
| 211 |
} |
| 212 |
else if( _file.isDirectory() ) |
| 213 |
{ |
| 214 |
String files[] = _file.list(); |
| 215 |
MimetypesFileTypeMap map = new MimetypesFileTypeMap(); |
| 216 |
map.addMimeTypes("text/java java\n"); |
| 217 |
FileTypeMap.setDefaultFileTypeMap(map); |
| 218 |
dh = new DataHandler[ files.length ]; |
| 219 |
// iterate through the list and make fds |
| 220 |
for( i = 0; i < files.length; i++) |
| 221 |
{ |
| 222 |
|
| 223 |
|
| 224 |
if(debug) |
| 225 |
System.out.println(files[i]); |
| 226 |
|
| 227 |
dh[i] = new DataHandler( |
| 228 |
new FileDataSource( _file.getAbsolutePath() + |
| 229 |
_file.separator + |
| 230 |
files[i] ) |
| 231 |
); |
| 232 |
dh[i].setCommandMap(cmdmap); |
| 233 |
|
| 234 |
// try { |
| 235 |
// ((FileDataSource)dh[i].getDataSource()).addMimeTypes("text/plain java\n"); |
| 236 |
// } catch(IOException e){ System.out.println(e); } |
| 237 |
file_list.add(dh[i].getName()); |
| 238 |
} |
| 239 |
System.out.println("number of files: " + files.length + |
| 240 |
" read : " + i); |
| 241 |
|
| 242 |
} |
| 243 |
|
| 244 |
} |
| 245 |
/////////////////***ACTION LISTENER***/////////////////////// |
| 246 |
// is called when a monkey (user) presses the launch button |
| 247 |
public void actionPerformed(ActionEvent evt) |
| 248 |
{ |
| 249 |
Object source = evt.getSource(); |
| 250 |
int index = file_list.getSelectedIndex(); |
| 251 |
|
| 252 |
// make sure it's the launch button and that |
| 253 |
// a list item is selected... |
| 254 |
if(source == launch_button && index > -1){ |
| 255 |
CommandInfo cmds[] = null; |
| 256 |
String mimeType = dh[index].getContentType(); |
| 257 |
|
| 258 |
if(debug) |
| 259 |
System.out.println("Finding..."); |
| 260 |
|
| 261 |
// get the available commands for this type |
| 262 |
cmds = dh[index].getPreferredCommands(); |
| 263 |
|
| 264 |
// if we actually got some back... |
| 265 |
if(cmds.length > 0){ |
| 266 |
CompViewer cont = null; |
| 267 |
Object my_bean = null; |
| 268 |
// take the first one |
| 269 |
if(debug) |
| 270 |
System.out.println("Launching..."); |
| 271 |
|
| 272 |
// get the first one |
| 273 |
my_bean = dh[index].getBean( cmds[verb_choice.getSelectedIndex()] ); |
| 274 |
|
| 275 |
// if it isn't a CommandObject we still |
| 276 |
// have to give it it's data... |
| 277 |
// if(!(my_bean instanceof jakarta.activation.CommandObject)) |
| 278 |
// { |
| 279 |
// System.out.println("WHOOOAAA!"); |
| 280 |
// if(my_bean instanceof |
| 281 |
// java.io.Externalizable) |
| 282 |
|
| 283 |
// { |
| 284 |
// try |
| 285 |
// { |
| 286 |
// ((Externalizable)my_bean).readExternal( |
| 287 |
// new ObjectInputStream( |
| 288 |
// dh[index].getInputStream() |
| 289 |
// ) |
| 290 |
// ); |
| 291 |
// } catch(Exception e) |
| 292 |
// { |
| 293 |
// System.out.println("There was a problem reading the Externalized Data in the viewer bean : " + e); |
| 294 |
// } |
| 295 |
// } |
| 296 |
// } |
| 297 |
if(debug) |
| 298 |
System.out.println("GOTTA BEAN!: " + |
| 299 |
my_bean.getClass().getName() ); |
| 300 |
// create a new bean container |
| 301 |
cont = new CompViewer( "JAF Component" ); |
| 302 |
cont.setBean( (Component) my_bean); |
| 303 |
cont.show(); |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
/////////////////***ITEM LISTENER***///////////////////////// |
| 308 |
public void itemStateChanged(ItemEvent evt) |
| 309 |
{ |
| 310 |
Integer id = (Integer)evt.getItem(); |
| 311 |
|
| 312 |
if(evt.getStateChange() == ItemEvent.SELECTED) |
| 313 |
{ |
| 314 |
// get the content type from the data handler... |
| 315 |
String mime_type = dh[id.intValue()].getContentType(); |
| 316 |
// set the state in the info dlog: |
| 317 |
f_name_label.setText(dh[id.intValue()].getName()); |
| 318 |
|
| 319 |
// set the field in the UI and enable launch button |
| 320 |
if(mime_type != null) |
| 321 |
{ |
| 322 |
CommandInfo cmds[] = null; |
| 323 |
// set the text label |
| 324 |
type_label.setText(mime_type); |
| 325 |
|
| 326 |
// check to see if any commands are available |
| 327 |
cmds = dh[id.intValue()].getPreferredCommands(); |
| 328 |
if(cmds != null && cmds.length > 0) // we got a command! |
| 329 |
{ |
| 330 |
launch_button.setEnabled( true ); |
| 331 |
verb_choice.setEnabled( true ); |
| 332 |
launch_button.setBackground( Color.green ); |
| 333 |
|
| 334 |
verb_choice.removeAll(); |
| 335 |
for(int i = 0; i < cmds.length; i++) |
| 336 |
{ |
| 337 |
// verb_choice.addItem(cmds[i].getBeanDescriptor().getName()); |
| 338 |
verb_choice.addItem(cmds[i].getCommandName()); |
| 339 |
} |
| 340 |
} |
| 341 |
else |
| 342 |
{ |
| 343 |
launch_button.setEnabled( false ); |
| 344 |
verb_choice.setEnabled( false ); |
| 345 |
launch_button.setBackground( Color.red ); |
| 346 |
} |
| 347 |
|
| 348 |
} |
| 349 |
else |
| 350 |
{ |
| 351 |
// set label to be unknown and launch button off! |
| 352 |
type_label.setText("<unknown>"); |
| 353 |
launch_button.setEnabled( false ); |
| 354 |
launch_button.setBackground( Color.red ); |
| 355 |
} |
| 356 |
|
| 357 |
} |
| 358 |
} |
| 359 |
/////////////////***WINDOW LISTENER***//////////////////////// |
| 360 |
public void windowOpened(WindowEvent e){} |
| 361 |
public void windowClosing(WindowEvent e) |
| 362 |
{ |
| 363 |
System.exit(0); // quit |
| 364 |
} |
| 365 |
public void windowClosed(WindowEvent e){} |
| 366 |
public void windowIconified(WindowEvent e){} |
| 367 |
public void windowDeiconified(WindowEvent e){} |
| 368 |
public void windowActivated(WindowEvent e){} |
| 369 |
public void windowDeactivated(WindowEvent e){} |
| 370 |
|
| 371 |
/////////////////////////////////////////////////////////////////////// |
| 372 |
// main |
| 373 |
public static void main(String args[]) |
| 374 |
{ |
| 375 |
JAFApp appFrame = new JAFApp("JAF Testing Application"); |
| 376 |
appFrame.setSize(600, 400); |
| 377 |
appFrame.show(); |
| 378 |
|
| 379 |
// set the directory |
| 380 |
if(args.length == 0) |
| 381 |
{ |
| 382 |
appFrame.setFile( new File(".")); |
| 383 |
} |
| 384 |
else |
| 385 |
{ |
| 386 |
appFrame.setFile( new File(args[0]) ); |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
} |