Repository navigation
Add word wrap action #9767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
abelhafi
wants to merge
1
commit into
apache:master
Choose a base branch
from
abelhafi:abelhafi/word-wrap-action
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add word wrap action #9767
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
203 changes: 203 additions & 0 deletions
203
ide/editor/src/org/netbeans/modules/editor/WordWrapAction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| /* | ||
| * 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.JCheckBoxMenuItem; | ||
| import javax.swing.JComponent; | ||
| 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.indent.spi.CodeStylePreferences; | ||
| 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.util.NbBundle.Messages; | ||
| import org.openide.util.WeakListeners; | ||
| import org.openide.windows.TopComponent; | ||
| import org.openide.windows.WindowManager; | ||
|
|
||
| /** | ||
| * Action that toggles word wrapping for the currently active editor instance. | ||
| * <p> | ||
| * This action appears as a checkbox menu item under the <b>View</b> menu. It | ||
| * allows users to enable or disable line wrapping dynamically for the current | ||
| * file without changing the global or MIME-type settings configured in | ||
| * {@code Tools/Options/Editor/Formatting}. | ||
| * </p> | ||
| * <p> | ||
| * Word wrapping behavior relies on the {@link LineWrapType} enumeration, which | ||
| * defines three values: {@code NONE}, {@code WORD_BOUND}, and | ||
| * {@code CHARACTER_BOUND}. This action specifically toggles between | ||
| * {@code NONE} and {@code WORD_BOUND}. If the editor is currently configured to | ||
| * use {@code CHARACTER_BOUND}, it will be treated as {@code NONE} by this | ||
| * action. | ||
| * </p> | ||
| * <p> | ||
| * The action is automatically disabled when it cannot be applied to the current | ||
| * context. It is bound to the {@code ALT-Z} keyboard shortcut by default, which | ||
| * can be reconfigured via {@code Tools/Options/Keymap}. | ||
| * </p> | ||
| * | ||
| * @author Alejandro L. Belhafi | ||
| */ | ||
| @Messages({"wordWrapAction.displayName=&Word Wrap"}) | ||
| @ActionID(category = "View", id = "org.netbeans.modules.editor.WordWrapAction") | ||
| @ActionReferences({ | ||
| @ActionReference(path = "Menu/View", position = 215), | ||
| @ActionReference(path = "Shortcuts", name = "O-Z")}) | ||
| @ActionRegistration(lazy = false, iconInMenu = false, displayName = "#wordWrapAction.displayName") | ||
| public class WordWrapAction extends AbstractAction | ||
| implements PropertyChangeListener, Runnable, DynamicMenuContent { | ||
|
|
||
| /** | ||
| * Constant representing no line wrapping, mapping to | ||
| * {@link LineWrapType#NONE}. This is also used as the fallback treatment for | ||
| * {@link LineWrapType#CHARACTER_BOUND}. | ||
| */ | ||
| private static final String NONE = "none"; //NOI18N | ||
|
|
||
| /** | ||
| * Constant representing standard word wrapping, mapping to | ||
| * {@link LineWrapType#WORD_BOUND}. | ||
| */ | ||
| private static final String WORDS = "words"; //NOI18N | ||
|
|
||
| private JCheckBoxMenuItem menu; | ||
|
|
||
| public WordWrapAction() { | ||
| super(Bundle.wordWrapAction_displayName()); | ||
| putValue(SELECTED_KEY, false); | ||
|
|
||
| TopComponent.getRegistry().addPropertyChangeListener( | ||
| WeakListeners.propertyChange(this, TopComponent.getRegistry())); | ||
| } | ||
|
|
||
| /** | ||
| * Toggles the word wrap state for the currently active editor and updates | ||
| * the checkbox selection status of the action accordingly. | ||
| * | ||
| * @param event the action event triggered by the user (e.g., clicking the | ||
| * View menu item or pressing the keyboard shortcut) | ||
| * @see DocumentViewOp#updateLineWrapType() | ||
| */ | ||
| @Override | ||
| public void actionPerformed(ActionEvent event) { | ||
|
|
||
| final JTextComponent editor = Utilities.getLastActiveComponent(); | ||
|
|
||
| if (editor == null) { | ||
| return; | ||
| } | ||
|
|
||
| boolean newSelection = !(isWordWrapActive(editor)); | ||
|
|
||
| putValue(SELECTED_KEY, newSelection); | ||
|
|
||
| editor.putClientProperty(SimpleValueNames.TEXT_LINE_WRAP, newSelection ? WORDS : NONE); | ||
| } | ||
|
|
||
| private String getLineWrapType(JTextComponent editor) { | ||
| String lineWrapType = (String) editor.getClientProperty(SimpleValueNames.TEXT_LINE_WRAP); | ||
|
|
||
| if (lineWrapType == null) { | ||
| lineWrapType = CodeStylePreferences.get(editor.getDocument()) | ||
| .getPreferences() | ||
| .get(SimpleValueNames.TEXT_LINE_WRAP, NONE); | ||
| } | ||
|
|
||
| return lineWrapType; | ||
| } | ||
|
|
||
| @Override | ||
| public JComponent[] getMenuPresenters() { | ||
| createMenu(); | ||
| updateState(); | ||
| return new JCheckBoxMenuItem[]{menu}; | ||
| } | ||
|
|
||
| private void createMenu() { | ||
| menu = new JCheckBoxMenuItem(); | ||
| Actions.connect(menu, this, true); | ||
| } | ||
|
|
||
| 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); | ||
| updateSelection(); | ||
| } | ||
| } | ||
|
|
||
| private void updateSelection() { | ||
| if (!isEnabled()) { | ||
| putValue(SELECTED_KEY, false); | ||
| return; | ||
| } | ||
|
|
||
| final JTextComponent editor = Utilities.getLastActiveComponent(); | ||
| if (editor == null) { | ||
| putValue(SELECTED_KEY, false); | ||
| return; | ||
| } | ||
|
|
||
| putValue(SELECTED_KEY, isWordWrapActive(editor)); | ||
| } | ||
|
|
||
| private boolean isWordWrapActive(JTextComponent editor) { | ||
| return WORDS.equals(getLineWrapType(editor)); | ||
| } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes sense to me, if we assume that we don't want to introduce new dependencies/API. But looking at
LineWrapType, the values encoded there are already API as they are used at multiple places and persitence. So from my POV, given thatfromSettingValuealready exists, I suggest to introducegetSettingValueto return that value. Drop the local definitions and deal withLineWrapTypewhere possible.