diff --git a/ide/editor.lib2/src/org/netbeans/modules/editor/lib2/view/LineWrapType.java b/ide/editor.lib2/src/org/netbeans/modules/editor/lib2/view/LineWrapType.java index 847e9f3b2834..3fc178faba2d 100644 --- a/ide/editor.lib2/src/org/netbeans/modules/editor/lib2/view/LineWrapType.java +++ b/ide/editor.lib2/src/org/netbeans/modules/editor/lib2/view/LineWrapType.java @@ -22,6 +22,7 @@ * Enumeration for line wrap type. * * @author mmetelka + * @author Alejandro L. Belhafi */ public enum LineWrapType { @@ -47,5 +48,9 @@ public static LineWrapType fromSettingValue(String settingValue) { } return null; } + + public String getSettingValue() { + return this.settingValue; + } } diff --git a/ide/editor/src/org/netbeans/modules/editor/LineWrapAction.java b/ide/editor/src/org/netbeans/modules/editor/LineWrapAction.java new file mode 100644 index 000000000000..c88882d39a2f --- /dev/null +++ b/ide/editor/src/org/netbeans/modules/editor/LineWrapAction.java @@ -0,0 +1,307 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.modules.editor; + +import java.awt.EventQueue; +import java.awt.event.ActionEvent; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import javax.swing.AbstractAction; +import javax.swing.Action; +import javax.swing.ButtonGroup; +import javax.swing.JComponent; +import javax.swing.JMenu; +import javax.swing.JRadioButtonMenuItem; +import javax.swing.SwingUtilities; +import javax.swing.text.JTextComponent; +import org.netbeans.api.editor.settings.SimpleValueNames; +import org.netbeans.editor.Utilities; +import org.netbeans.modules.editor.lib2.view.DocumentViewOp; +import org.netbeans.modules.editor.lib2.view.LineWrapType; +import org.openide.awt.ActionID; +import org.openide.awt.ActionReference; +import org.openide.awt.ActionReferences; +import org.openide.awt.ActionRegistration; +import org.openide.awt.Actions; +import org.openide.awt.DynamicMenuContent; +import org.openide.awt.Mnemonics; +import org.openide.util.NbBundle.Messages; +import org.openide.util.WeakListeners; +import org.openide.windows.TopComponent; +import org.openide.windows.WindowManager; + +/** + * Registers a "Line Wrap" submenu under the "View" menu to control editor line + * wrapping. + * + *
+ * The "Line Wrap" submenu provides three items corresponding to the actions for + * each wrapping mode: wrap off, wrap anywhere, and wrap after words.
+ * + *+ * Each of these three actions supports customizable keyboard shortcuts + * configured under {@code Tools > Options > Keymap}. By default, only the wrap + * after words action is bound to {@code ALT-Z}.
+ * + * @author Alejandro L. Belhafi + */ +@Messages({ + "lineWrapAction.displayName=Line Wrap", + "lineWrapAction.menu=Line &Wrap", + "lineWrapAction.menuItem.off=&Off", + "lineWrapAction.menuItem.anywhere=&Anywhere", + "lineWrapAction.menuItem.afterWords=After &words", + "lineWrapAction.wrapOffAction.displayName=Wrap Off", + "lineWrapAction.wrapAfterWordsAction.displayName=Wrap After Words", + "lineWrapAction.wrapAnywhereAction.displayName=Wrap Anywhere" +}) +@ActionID( + category = LineWrapAction.CATEGORY, + id = LineWrapAction.ID) +@ActionReferences({ + @ActionReference( + path = "Menu/View", + position = 215)}) +@ActionRegistration( + lazy = false, + iconInMenu = false, + displayName = "#lineWrapAction.displayName") +public final class LineWrapAction extends AbstractAction + implements PropertyChangeListener, Runnable, DynamicMenuContent { + + public static final String ID = "org.netbeans.modules.editor.LineWrapAction"; + public static final String CATEGORY = "View"; + public static final String SUBCATEGORY = CATEGORY + "/Line Wrap"; + + private static final long serialVersionUID = 1L; + + private JMenu menu; + private JRadioButtonMenuItem wrapOffMenuItem; + private JRadioButtonMenuItem wrapAnywhereMenuItem; + private JRadioButtonMenuItem wrapAfterWordsMenuItem; + + public LineWrapAction() { + super(Bundle.lineWrapAction_displayName()); + + TopComponent.getRegistry().addPropertyChangeListener( + WeakListeners.propertyChange(this, TopComponent.getRegistry())); + } + + @Override + public void actionPerformed(ActionEvent event) { + // Do nothing + } + + /** + * Gets the line wrap type currently configured for the active editor. + * + * @return the current line wrap type of the active editor, or an empty + * string if there is no active editor. + */ + private static String getLineWrapTypeForActiveEditor() { + final JTextComponent editor = Utilities.getLastActiveComponent(); + + if (editor == null) { + return ""; + } + + String lineWrapType = (String) editor.getClientProperty(SimpleValueNames.TEXT_LINE_WRAP); + + if (lineWrapType == null) { + lineWrapType = LineWrapType.NONE.getSettingValue(); + } + + return lineWrapType; + + } + + /** + * Sets the line wrap type for the active editor. + * + * @param lineWrapType the line wrap type to apply to the editor. + * @see DocumentViewOp#updateLineWrapType() + */ + private static void setLineWrapTypeForActiveEditor(LineWrapType lineWrapType) { + final JTextComponent editor = Utilities.getLastActiveComponent(); + + if (editor == null) { + return; + } + + editor.putClientProperty(SimpleValueNames.TEXT_LINE_WRAP, lineWrapType.getSettingValue()); + } + + @Override + public JComponent[] getMenuPresenters() { + createMenu(); + updateState(); + return new JComponent[]{menu}; + } + + private void createMenu() { + + Action action = Actions.forID(LineWrapAction.SUBCATEGORY, WrapOffAction.ID); + wrapOffMenuItem = new JRadioButtonMenuItem(action); + Mnemonics.setLocalizedText(wrapOffMenuItem, Bundle.lineWrapAction_menuItem_off()); + + action = Actions.forID(LineWrapAction.SUBCATEGORY, WrapAnywhereAction.ID); + wrapAnywhereMenuItem = new JRadioButtonMenuItem(action); + Mnemonics.setLocalizedText(wrapAnywhereMenuItem, Bundle.lineWrapAction_menuItem_anywhere()); + + action = Actions.forID(LineWrapAction.SUBCATEGORY, WrapAfterWordsAction.ID); + wrapAfterWordsMenuItem = new JRadioButtonMenuItem(action); + Mnemonics.setLocalizedText(wrapAfterWordsMenuItem, Bundle.lineWrapAction_menuItem_afterWords()); + + ButtonGroup group = new ButtonGroup(); + group.add(wrapOffMenuItem); + group.add(wrapAnywhereMenuItem); + group.add(wrapAfterWordsMenuItem); + + menu = new JMenu(this); + Mnemonics.setLocalizedText(menu, Bundle.lineWrapAction_menu()); + menu.add(wrapOffMenuItem); + menu.add(wrapAnywhereMenuItem); + menu.add(wrapAfterWordsMenuItem); + } + + private void updateState() { + if (EventQueue.isDispatchThread()) { + run(); + } else { + SwingUtilities.invokeLater(this); + } + } + + @Override + public JComponent[] synchMenuPresenters(JComponent[] items) { + updateState(); + return items; + } + + @Override + public void propertyChange(PropertyChangeEvent evt) { + if (TopComponent.Registry.PROP_ACTIVATED.equals(evt.getPropertyName())) { + updateState(); + } + } + + @Override + public void run() { + boolean isDocumentActive = false; + TopComponent tc = TopComponent.getRegistry().getActivated(); + if (tc != null) { + isDocumentActive = WindowManager.getDefault().isEditorTopComponent(tc); + } + synchronized (this) { + setEnabled(isDocumentActive); + + if (isDocumentActive) { + updateSelectedMenu(); + } + } + } + + private void updateSelectedMenu() { + String lineWrapType = getLineWrapTypeForActiveEditor(); + + if (LineWrapType.NONE.getSettingValue().equals(lineWrapType)) { + wrapOffMenuItem.setSelected(true); + } else if (LineWrapType.CHARACTER_BOUND.getSettingValue().equals(lineWrapType)) { + wrapAnywhereMenuItem.setSelected(true); + } else if (LineWrapType.WORD_BOUND.getSettingValue().equals(lineWrapType)) { + wrapAfterWordsMenuItem.setSelected(true); + } + } + + @ActionID( + category = LineWrapAction.SUBCATEGORY, + id = WrapOffAction.ID) + @ActionRegistration( + lazy = false, + iconInMenu = false, + displayName = "#lineWrapAction.wrapOffAction.displayName") + public static final class WrapOffAction extends AbstractAction { + + public static final String ID = "org.netbeans.modules.editor.LineWrapAction.WrapOffAction"; + + private static final long serialVersionUID = 1L; + + public WrapOffAction() { + super(Bundle.lineWrapAction_wrapOffAction_displayName()); + } + + @Override + public void actionPerformed(ActionEvent e) { + setLineWrapTypeForActiveEditor(LineWrapType.NONE); + } + + } + + @ActionID( + category = LineWrapAction.SUBCATEGORY, + id = WrapAnywhereAction.ID) + @ActionRegistration( + lazy = false, + iconInMenu = false, + displayName = "#lineWrapAction.wrapAnywhereAction.displayName") + public static final class WrapAnywhereAction extends AbstractAction { + + public static final String ID = "org.netbeans.modules.editor.LineWrapAction.WrapAnywhereAction"; + + private static final long serialVersionUID = 1L; + + public WrapAnywhereAction() { + super(Bundle.lineWrapAction_wrapAnywhereAction_displayName()); + } + + @Override + public void actionPerformed(ActionEvent e) { + setLineWrapTypeForActiveEditor(LineWrapType.CHARACTER_BOUND); + } + + } + + @ActionID( + category = LineWrapAction.SUBCATEGORY, + id = WrapAfterWordsAction.ID) + @ActionReferences({ + @ActionReference( + path = "Shortcuts", + name = "O-Z")}) + @ActionRegistration( + lazy = false, + iconInMenu = false, + displayName = "#lineWrapAction.wrapAfterWordsAction.displayName") + public static final class WrapAfterWordsAction extends AbstractAction { + + public static final String ID = "org.netbeans.modules.editor.LineWrapAction.WrapAfterWordsAction"; + + private static final long serialVersionUID = 1L; + + public WrapAfterWordsAction() { + super(Bundle.lineWrapAction_wrapAfterWordsAction_displayName()); + } + + @Override + public void actionPerformed(ActionEvent e) { + setLineWrapTypeForActiveEditor(LineWrapType.WORD_BOUND); + } + + } +}