private Action getAction(String name) { for (Action action : sourceArea.getActions()) { if (name.equals(action.getValue(Action.NAME).toString())) { return action; } } return null; }
@SuppressWarnings("OverridableMethodCallInConstructor") Notepad() { super(true); // Trying to set Nimbus look and feel try { for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (Exception ignored) { } setBorder(BorderFactory.createEtchedBorder()); setLayout(new BorderLayout()); // create the embedded JTextComponent editor = createEditor(); // Add this as a listener for undoable edits. editor.getDocument().addUndoableEditListener(undoHandler); // install the command table commands = new HashMap<Object, Action>(); Action[] actions = getActions(); for (Action a : actions) { commands.put(a.getValue(Action.NAME), a); } JScrollPane scroller = new JScrollPane(); JViewport port = scroller.getViewport(); port.add(editor); String vpFlag = getProperty("ViewportBackingStore"); if (vpFlag != null) { Boolean bs = Boolean.valueOf(vpFlag); port.setScrollMode(bs ? JViewport.BACKINGSTORE_SCROLL_MODE : JViewport.BLIT_SCROLL_MODE); } JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); panel.add("North", createToolbar()); panel.add("Center", scroller); add("Center", panel); add("South", createStatusbar()); }
/** * Registers the keystroke of the given action as "command" of the given component. * * <p>This code is based on the Sulky-tools, found at <http://github.com/huxi/sulky>. * * @param aComponent the component that should react on the keystroke, cannot be <code>null</code> * ; * @param aAction the action of the keystroke, cannot be <code>null</code>; * @param aCommandName the name of the command to register the keystore under. */ public static void registerKeystroke( final JComponent aComponent, final Action aAction, final String aCommandName) { final KeyStroke keyStroke = (KeyStroke) aAction.getValue(Action.ACCELERATOR_KEY); if (keyStroke == null) { return; } InputMap inputMap = aComponent.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); ActionMap actionMap = aComponent.getActionMap(); inputMap.put(keyStroke, aCommandName); actionMap.put(aCommandName, aAction); inputMap = aComponent.getInputMap(JComponent.WHEN_FOCUSED); Object value = inputMap.get(keyStroke); if (value != null) { inputMap.put(keyStroke, aCommandName); } inputMap = aComponent.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); value = inputMap.get(keyStroke); if (value != null) { inputMap.put(keyStroke, aCommandName); } }
// ------------------------------------------------------------------------ TextViewer(JFrame inParentFrame) { // super(true); //is double buffered - only for panels textViewerFrame = this; parentFrame = inParentFrame; lastViewedDirStr = ""; lastViewedFileStr = ""; setTitle(resources.getString("Title")); addWindowListener(new AppCloser()); pack(); setSize(500, 600); warningPopup = new WarningDialog(this); okCancelPopup = new WarningDialogOkCancel(this); messagePopup = new MessageDialog(this); okCancelMessagePopup = new MessageDialogOkCancel(this); // Force SwingSet to come up in the Cross Platform L&F try { UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); // If you want the System L&F instead, comment out the above line and // uncomment the following: // UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception exc) { String errstr = "TextViewer:Error loading L&F: " + exc; warningPopup.display(errstr); } Container cf = getContentPane(); cf.setBackground(Color.lightGray); // Border etched=BorderFactory.createEtchedBorder(); // Border title=BorderFactory.createTitledBorder(etched,"TextViewer"); // cf.setBorder(title); cf.setLayout(new BorderLayout()); // create the embedded JTextComponent editor1 = createEditor(); editor1.setFont(new Font("monospaced", Font.PLAIN, 12)); // aa -added next line setPlainDocument((PlainDocument) editor1.getDocument()); // sets doc1 // install the command table commands = new Hashtable(); Action[] actions = getActions(); for (int i = 0; i < actions.length; i++) { Action a = actions[i]; commands.put(a.getValue(Action.NAME), a); // System.out.println("Debug:TextViewer: actionName:"+a.getValue(Action.NAME)); } // editor1.setPreferredSize(new Dimension(,)); // get setting from user preferences if (UserPref.keymapType.equals("Word")) { editor1 = updateKeymapForWord(editor1); } else { editor1 = updateKeymapForEmacs(editor1); } scroller1 = new JScrollPane(); viewport1 = scroller1.getViewport(); viewport1.add(editor1); scroller1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); scroller1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); try { String vpFlag = resources.getString("ViewportBackingStore"); Boolean bs = new Boolean(vpFlag); viewport1.setBackingStoreEnabled(bs.booleanValue()); } catch (MissingResourceException mre) { System.err.println("TextViewer:missing resource:" + mre.getMessage()); // just use the viewport1 default } menuItems = new Hashtable(); menubar = createMenubar(); lowerPanel = new JPanel(true); // moved double buffering to here lowerPanel.setLayout(new BorderLayout()); lowerPanel.add("North", createToolbar()); lowerPanel.add("Center", scroller1); cf.add("North", menubar); cf.add("Center", lowerPanel); cf.add("South", createStatusbar()); // for the find/search utilities mySearchDialog = new SearchDialog(this); // System.out.println("Debug:TextViewer: end of TextViewer constructor"); }