1 11 12 package org.enhydra.jawe; 13 14 import java.awt.*; 15 import java.awt.event.*; 16 import javax.swing.*; 17 import javax.swing.text.*; 18 import java.util.*; 19 import java.beans.*; 20 import java.net.URL ; 21 import org.enhydra.jawe.actions.*; 22 23 public class BarFactory { 24 25 protected AbstractEditor editor; 26 27 public BarFactory( AbstractEditor editor ) { 28 this.editor = editor; 29 } 30 31 39 public Component createToolBars( JTabbedPane tabbedPane ) { 40 String [] toolBars = Utils.tokenize( ResourceManager.getResourceString( editor.toolbarToLoad() ), 41 " " ); 42 JPanel lastPanel = new JPanel(); 43 lastPanel.setLayout( new BorderLayout() ); 44 int i; 45 for ( i = 0; i < toolBars.length; i++ ) { 46 if ( !toolBars[i].equals( "-" ) ) { 48 String label = ResourceManager.getLanguageDependentString( toolBars[i] + 49 JaWEConstants.LABEL_SUFFIX ); 50 final JPanel panel = new JPanel(); 51 panel.setLayout( new BorderLayout() ); 52 final Component c = createToolbar( toolBars[i], label ); 53 panel.add( BorderLayout.WEST, c ); 54 ImageIcon icon = null; 55 URL url = ResourceManager.getResource( toolBars[i] + JaWEConstants.IMAGE_SUFFIX ); 56 if ( url != null ) { 57 icon = new ImageIcon( url ); 58 } 59 tabbedPane.addTab( label, icon, panel, label ); 60 } 61 else { 62 break; 63 } 64 } 65 tabbedPane.setPreferredSize( new Dimension( 500, 60 ) ); 66 tabbedPane.setSelectedIndex( i - 1 ); 67 68 lastPanel.add( BorderLayout.WEST, tabbedPane ); 69 if ( i < toolBars.length - 1 ) { 70 Component c = createTool( toolBars[i + 1] ); 71 lastPanel.add( BorderLayout.EAST, c ); 72 } 73 74 return lastPanel; 75 } 76 77 82 protected Component createToolbar( String key, String label ) { 83 JToolBar toolbar = new JToolBar( label ); 84 toolbar.putClientProperty( "JToolBar.isRollover", Boolean.TRUE ); 85 String [] toolKeys = Utils.tokenize( ResourceManager.getResourceString( key ), " " ); 87 for ( int i = 0; i < toolKeys.length; i++ ) { 88 if ( toolKeys[i].equals( "-" ) ) { 89 toolbar.addSeparator(); } 91 else { 92 toolbar.add( createTool( toolKeys[i] ) ); 93 } 94 } 95 toolbar.add( Box.createHorizontalGlue() ); 96 return toolbar; 97 } 98 99 103 protected Component createTool( String key ) { 104 return createToolbarButton( key ); 105 } 106 107 116 protected Component createToolbarButton( String key ) { 117 Object specialButton = editor.getSpecialButton( key ); 118 if ( specialButton instanceof JComponent && 119 ! ( specialButton instanceof AbstractButton ) ) { 120 return ( JComponent ) specialButton; 121 } 122 else { 123 AbstractButton b = null; 124 URL url = ResourceManager.getResource( key + JaWEConstants.IMAGE_SUFFIX ); 125 if ( specialButton instanceof AbstractButton ) { 126 b = ( AbstractButton ) specialButton; 127 b.setIcon( new ImageIcon( url ) ); 128 } 129 else { 130 if ( url != null ) { 131 b = new JButton( new ImageIcon( url ) ) { 132 public float getAlignmentY() { 133 return 0.5f; 134 } 135 }; 136 } 137 else { 138 b = new JButton( ResourceManager.getLanguageDependentString( key + 139 JaWEConstants.LABEL_SUFFIX ) ) { 140 public float getAlignmentY() { 141 return 0.5f; 142 } 143 }; 144 } 145 } 146 b.setMargin( new Insets( 1, 1, 1, 1 ) ); 147 b.setRequestFocusEnabled( false ); 148 149 if ( specialButton == null ) { 150 String astr = ResourceManager.getResourceString( key + JaWEConstants.ACTION_SUFFIX ); 151 if ( astr == null ) { 152 astr = key; 153 } 154 Action a = editor.getAction( astr ); 155 if ( a != null ) { 156 b.setActionCommand( astr ); 157 b.addActionListener( new BarFactory.EventRedirector( a ) ); 158 b.setEnabled( a.isEnabled() ); 159 a.addPropertyChangeListener( new BarFactory.ActionChangedListener( b ) ); 160 } 161 else { 162 b.setEnabled( false ); 163 } 164 } 165 166 String tip = ResourceManager.getLanguageDependentString( key + JaWEConstants.TIP_SUFFIX ); 167 if ( tip != null ) { 168 b.setToolTipText( tip ); 169 } 170 editor.putToolbarComponent( key, b ); 171 return b; 172 } 173 } 174 175 180 public JMenuBar createMenubar() { 181 JMenuItem mi; 182 JMenuBar mb = new JMenuBar(); 183 String [] menuKeys = Utils.tokenize( ResourceManager.getResourceString( editor.menubarToLoad() ), 184 " " ); 185 for ( int i = 0; i < menuKeys.length; i++ ) { 186 String [] itemKeys = Utils.tokenize( ResourceManager.getResourceString( menuKeys[i] ), " " ); 187 JMenu m = createMenu( menuKeys[i], itemKeys ); 188 if ( m != null ) { 189 mb.add( m ); 190 } 191 } 192 193 return mb; 194 } 195 196 200 public JMenuBar createTextViewMenubar( String key ) { 201 202 JMenuItem mi; 203 JMenuBar mb = new JMenuBar(); 204 String [] menuKeys = Utils.tokenize( ResourceManager.getResourceString( key ), " " ); 205 for ( int i = 0; i < menuKeys.length; i++ ) { 206 String [] itemKeys = Utils.tokenize( ResourceManager.getResourceString( menuKeys[i] ), " " ); 207 JMenu m = createTextViewMenu( menuKeys[i], itemKeys ); 208 if ( m != null ) { 209 mb.add( m ); 210 } 211 } 212 return mb; 213 } 214 215 219 public JMenu createTextViewMenu( String key, String [] itemKeys ) { 220 JMenu menu = new JMenu( ResourceManager.getLanguageDependentString( key + 221 JaWEConstants.LABEL_SUFFIX ) ); 222 for ( int i = 0; i < itemKeys.length; i++ ) { 223 if ( itemKeys[i].equals( "-" ) ) { 224 menu.addSeparator(); 225 } 226 else { 227 JMenuItem mi = createTextVeiwMenuItem( itemKeys[i] ); 228 menu.add( mi ); 229 } 230 } 231 URL url = ResourceManager.getResource( key + JaWEConstants.IMAGE_SUFFIX ); 232 if ( url != null ) { 233 menu.setHorizontalTextPosition( JButton.RIGHT ); 234 menu.setIcon( new ImageIcon( url ) ); 235 } 236 237 setMnemonic( menu, 238 ResourceManager.getLanguageDependentString( key + JaWEConstants.MNEMONIC_SUFFIX ) ); 239 240 menu.setActionCommand( key ); 241 242 return menu; 243 } 244 245 248 public JMenuItem createTextVeiwMenuItem( String cmd ) { 249 String subMenu = ResourceManager.getResourceString( cmd + JaWEConstants.MENU_SUFFIX ); 250 if ( subMenu != null ) { 251 String [] itemKeys = Utils.tokenize( subMenu, " " ); 252 JMenu mn = createTextViewMenu( cmd, itemKeys ); 253 return mn; 254 } 255 else { 256 JMenuItem mi; 257 mi = new JMenuItem( ResourceManager.getLanguageDependentString( cmd + 258 JaWEConstants.LABEL_SUFFIX ) ); 259 260 URL url = ResourceManager.getResource( cmd + JaWEConstants.IMAGE_SUFFIX ); 261 if ( url != null ) { 262 mi.setHorizontalTextPosition( JButton.RIGHT ); 263 mi.setIcon( new ImageIcon( url ) ); 264 } 265 266 setAccelerator( mi, 267 ResourceManager.getLanguageDependentString( cmd + 268 JaWEConstants.ACCEL_SUFFIX ) ); 269 setMnemonic( mi, 270 ResourceManager.getLanguageDependentString( cmd + 271 JaWEConstants.MNEMONIC_SUFFIX ) ); 272 273 String astr = ResourceManager.getResourceString( cmd + JaWEConstants.ACTION_SUFFIX ); 274 if ( astr == null ) { 275 astr = cmd; 276 } 277 mi.setActionCommand( astr ); 278 Action a = getTextViewAction( astr ); 279 if ( a != null ) { 280 mi.addActionListener( a ); 281 mi.setEnabled( a.isEnabled() ); 282 } 283 else { 284 mi.setEnabled( false ); 285 } 286 287 return mi; 288 } 289 } 290 291 293 Hashtable commands = new Hashtable(); 294 295 public Action getTextViewAction( String key ) { 296 return ( Action ) commands.get( key ); 297 } 298 299 public void createViewActions(JTextComponent textComponent) { 300 Action[] actions = { 301 new TextSaveAs(textComponent), new TextPrint() }; 302 for ( int i = 0; i < actions.length; i++ ) { 303 Action a = actions[i]; 304 commands.put( a.getValue( Action.NAME ), a ); 305 } 306 } 307 308 315 protected JMenu createMenu( String key, String [] itemKeys ) { 316 JMenu menu = new JMenu( ResourceManager.getLanguageDependentString( key + 317 JaWEConstants.LABEL_SUFFIX ) ); 318 for ( int i = 0; i < itemKeys.length; i++ ) { 319 if ( itemKeys[i].equals( "-" ) ) { 320 menu.addSeparator(); 321 } 322 else { 323 JMenuItem mi = createMenuItem( itemKeys[i], true ); 324 menu.add( mi ); 325 } 326 } 327 URL url = ResourceManager.getResource( key + JaWEConstants.IMAGE_SUFFIX ); 328 if ( url != null ) { 329 menu.setHorizontalTextPosition( JButton.RIGHT ); 330 menu.setIcon( new ImageIcon( url ) ); 331 } 332 333 setMnemonic( menu, 334 ResourceManager.getLanguageDependentString( key + JaWEConstants.MNEMONIC_SUFFIX ) ); 335 336 menu.setActionCommand( key ); 337 338 return menu; 339 } 340 341 346 public JMenuItem createMenuItem( String cmd, boolean putIntoTable ) { 347 String subMenu = ResourceManager.getResourceString( cmd + JaWEConstants.MENU_SUFFIX ); 348 if ( subMenu != null ) { 349 String [] itemKeys = Utils.tokenize( subMenu, " " ); 350 JMenu mn = createMenu( cmd, itemKeys ); 351 if ( putIntoTable ) { 352 editor.putMenuItem( cmd, mn ); 353 } 354 return mn; 355 } 356 else { 357 JMenuItem mi; 358 Object specialItem = editor.getSpecialItem( cmd ); 359 if ( specialItem instanceof JMenuItem ) { 360 mi = ( JMenuItem ) specialItem; 361 mi.setText( ResourceManager.getLanguageDependentString( cmd + 362 JaWEConstants.LABEL_SUFFIX ) ); 363 } 364 else { 365 mi = new JMenuItem( ResourceManager.getLanguageDependentString( cmd + 366 JaWEConstants.LABEL_SUFFIX ) ); 367 } 368 URL url = ResourceManager.getResource( cmd + JaWEConstants.IMAGE_SUFFIX ); 369 if ( url != null ) { 370 mi.setHorizontalTextPosition( JButton.RIGHT ); 371 mi.setIcon( new ImageIcon( url ) ); 372 } 373 374 setAccelerator( mi, 375 ResourceManager.getLanguageDependentString( cmd + JaWEConstants.ACCEL_SUFFIX ) ); 376 setMnemonic( mi, 377 ResourceManager.getLanguageDependentString( cmd + JaWEConstants.MNEMONIC_SUFFIX ) ); 378 379 if ( specialItem == null ) { 380 String astr = ResourceManager.getResourceString( cmd + JaWEConstants.ACTION_SUFFIX ); 381 if ( astr == null ) { 382 astr = cmd; 383 } 384 mi.setActionCommand( astr ); 385 Action a = editor.getAction( astr ); 386 if ( a != null ) { 387 mi.addActionListener( new BarFactory.EventRedirector( a ) ); 388 a.addPropertyChangeListener( new BarFactory.ActionChangedListener( mi ) ); 389 mi.setEnabled( a.isEnabled() ); 390 } 391 else { 392 mi.setEnabled( false ); 393 } 394 } 395 396 if ( putIntoTable ) { 397 editor.putMenuItem( cmd, mi ); 398 } 399 return mi; 400 } 401 } 402 403 406 public static JMenuItem createMenuItem( String cmd ) { 407 JMenuItem mi; 408 mi = new JMenuItem( ResourceManager.getLanguageDependentString( cmd + 409 JaWEConstants.LABEL_SUFFIX ) ); 410 URL url = ResourceManager.getResource( cmd + JaWEConstants.IMAGE_SUFFIX ); 411 if ( url != null ) { 412 mi.setHorizontalTextPosition( JButton.RIGHT ); 413 mi.setIcon( new ImageIcon( url ) ); 414 } 415 return mi; 416 } 417 418 public static void setMnemonic( JMenuItem mi, String mnemonic ) { 419 if ( mnemonic != null && mnemonic.length() > 0 ) { 420 mi.setMnemonic( mnemonic.toCharArray()[0] ); 421 } 422 } 423 424 public static void setAccelerator( JMenuItem mi, String accel ) { 425 if ( accel != null ) { 426 try { 427 int mask = 0; 428 if ( accel.startsWith( "CTRL" ) ) { 429 mask += ActionEvent.CTRL_MASK; 430 accel = accel.substring( 5 ); 431 } 432 if ( accel.startsWith( "SHIFT" ) ) { 433 mask += ActionEvent.SHIFT_MASK; 434 accel = accel.substring( 6 ); 435 } 436 if ( accel.startsWith( "ALT" ) ) { 437 mask += ActionEvent.ALT_MASK; 438 accel = accel.substring( 4 ); 439 } 440 int key = KeyEvent.class.getField( "VK_" + accel ).getInt( null ); 441 mi.setAccelerator( KeyStroke.getKeyStroke( key, mask ) ); 442 } 443 catch ( Exception e ) { 444 System.err.println( "Error while assigning accelerator !!!" ); 445 } 446 } 447 } 448 449 451 private class EventRedirector 452 implements ActionListener { 453 454 protected Action action; 455 456 public EventRedirector( Action a ) { 457 this.action = a; 458 } 459 460 public void actionPerformed( ActionEvent e ) { 461 e = new ActionEvent( editor.getGraph(), e.getID(), e.getActionCommand(), e.getModifiers() ); 462 action.actionPerformed( e ); 463 } 464 } 465 466 468 472 private class ActionChangedListener 473 implements PropertyChangeListener { 474 AbstractButton button; 475 476 ActionChangedListener( AbstractButton b ) { 477 super(); 478 button = b; 479 } 480 481 public void propertyChange( PropertyChangeEvent e ) { 482 String propertyName = e.getPropertyName(); 483 if ( e.getPropertyName().equals( Action.NAME ) && 484 button instanceof JMenuItem ) { 485 String text = ( String ) e.getNewValue(); 486 button.setText( text ); 487 } 488 else { 489 if ( propertyName.equals( "enabled" ) ) { 490 Boolean enabledState = ( Boolean ) e.getNewValue(); 491 button.setEnabled( enabledState.booleanValue() ); 492 } 493 } 494 } 495 } 496 498 } 499 | Popular Tags |