1 10 11 import java.awt.*; 12 import java.awt.event.*; 13 import java.beans.*; 14 import java.io.*; 15 import javax.activation.*; 16 17 public class JAFApp extends Frame implements WindowListener, ItemListener, ActionListener { 18 19 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 private File _file = null; 32 private DataHandler dh[] = null; 34 35 private boolean debug = false; 36 public JAFApp(String title) 40 { 41 super(title); 42 initMe(); 43 } 44 45 public JAFApp() 46 { 47 super(); 48 initMe(); 49 } 50 51 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 = 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 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 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 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 = 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 = 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_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 = 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 this.addWindowListener(this); 162 file_list.addItemListener(this); 163 launch_button.addActionListener(this); 164 165 } 166 167 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 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 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 file_list.add(dh[i].getName()); 238 } 239 System.out.println("number of files: " + files.length + 240 " read : " + i); 241 242 } 243 244 } 245 public void actionPerformed(ActionEvent evt) 248 { 249 Object source = evt.getSource(); 250 int index = file_list.getSelectedIndex(); 251 252 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 cmds = dh[index].getPreferredCommands(); 263 264 if(cmds.length > 0){ 266 CompViewer cont = null; 267 Object my_bean = null; 268 if(debug) 270 System.out.println("Launching..."); 271 272 my_bean = dh[index].getBean( cmds[verb_choice.getSelectedIndex()] ); 274 275 283 if(debug) 298 System.out.println("GOTTA BEAN!: " + 299 my_bean.getClass().getName() ); 300 cont = new CompViewer( "JAF Component" ); 302 cont.setBean( (Component) my_bean); 303 cont.show(); 304 } 305 } 306 } 307 public void itemStateChanged(ItemEvent evt) 309 { 310 Integer id = (Integer )evt.getItem(); 311 312 if(evt.getStateChange() == ItemEvent.SELECTED) 313 { 314 String mime_type = dh[id.intValue()].getContentType(); 316 f_name_label.setText(dh[id.intValue()].getName()); 318 319 if(mime_type != null) 321 { 322 CommandInfo cmds[] = null; 323 type_label.setText(mime_type); 325 326 cmds = dh[id.intValue()].getPreferredCommands(); 328 if(cmds != null && cmds.length > 0) { 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].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 type_label.setText("<unknown>"); 353 launch_button.setEnabled( false ); 354 launch_button.setBackground( Color.red ); 355 } 356 357 } 358 } 359 public void windowOpened(WindowEvent e){} 361 public void windowClosing(WindowEvent e) 362 { 363 System.exit(0); } 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 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 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 } 392 | Popular Tags |