Skip to content

Commit

Permalink
Update wrap controls and synchronization with properties page
Browse files Browse the repository at this point in the history
This commit adds the following functionality:
- The wrap button is now a three-states button. Clicking it cycles between
  the states disabled/soft/hard
- The states are distinguished by icons of different color
- Button state is synchronized with the property page (and vice-versa)

Signed-off-by: Leonardo Montecchi <lmontecchi@montex.org>
  • Loading branch information
montex authored and ruspl-afed committed Mar 2, 2024
1 parent e2a07d4 commit 5f582bd
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 39 deletions.
12 changes: 7 additions & 5 deletions org.eclipse.texlipse/html/wrapping.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ <h1>Wrapping text</h1>

<h2>Use and Configuration:</h2>

<p>The workbench menu contains a toggle button for text wrapping. The toggle
button is visible when at least one .tex-file is open. Wrapping is set
<strong>on</strong> and <strong>off</strong> from this toggle. If the toggle
button is down then word wrap is used. Releasing the toggle disables
word wrap.</p>
<p>The workbench menu contains a button for text wrapping. The button is
visible when at least one .tex-file is open. The button is used to
cycle between the modes <strong>off</strong> (word wrap disabled),
<strong>soft wrap</strong> and <strong>hard wrap</strong>. If the toggle
button is down then word wrap is used; the icon changes its color based on the
active mode: green for soft wrap, red for hard wrap. The description of the
two modes is provided below.</p>

<p><img src="images/wrapButton.gif" alt="Wrap toggle button"></p>

Expand Down
Binary file added org.eclipse.texlipse/icons/wrap-hard.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added org.eclipse.texlipse/icons/wrap-soft.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion org.eclipse.texlipse/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,6 @@ ${cursor}
<action
class="org.eclipse.texlipse.actions.TexWordWrapAction"
definitionId="org.eclipse.texlipse.commands.texWordWrap"
icon="icons/wrap.gif"
id="org.eclipse.texlipse.actions.texWordWrap"
label="W&amp;rap text"
menubarPath="org.eclipse.texlipse.menus.latex/latexGroup"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
*******************************************************************************/
package org.eclipse.texlipse.actions;

import java.net.URL;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
Expand All @@ -23,20 +28,30 @@
import org.eclipse.ui.IActionDelegate2;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorPart;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;


/**
* Listens for word wrap toggle -actions, toggling wrap on or off.
*
* @author Laura Takkinen
* @author Oskar Ojala
* @author Leonardo Montecchi
*/
public class TexWordWrapAction implements IEditorActionDelegate, IActionDelegate2 {
private IEditorPart targetEditor;
private boolean off;
private IAction sourceButton;

private boolean enabled;
private boolean hard;

private String ICON_WRAP = "icons/wrap.gif";
private String ICON_WRAP_SOFT = "icons/wrap-soft.gif";
private String ICON_WRAP_HARD = "icons/wrap-hard.gif";

public TexWordWrapAction() {
this.off = !TexlipsePlugin.getDefault().getPreferenceStore().getBoolean(TexlipseProperties.WORDWRAP_DEFAULT);
loadWrapConfig();
TexlipsePlugin.getDefault().getPreferenceStore().addPropertyChangeListener(new WrapPropertyChangeListener());
}

Expand All @@ -53,29 +68,20 @@ private class WrapPropertyChangeListener implements IPropertyChangeListener {
*/
public void propertyChange(PropertyChangeEvent event) {
String ev = event.getProperty();
if (ev.equals("wrapType")) {
configureWordWrap();
if (ev.equals("wrapType") || ev.equals("wrapDefault")) {
loadWrapConfig();
setWrap();
}
}
}

/**
* Performs the operations needed on wrap configuration changes
*
* @author Leonardo Montecchi
*/
private void configureWordWrap() {
if (!off) {
setType();
}
}

/* (non-Javadoc)
* @see org.eclipse.ui.IActionDelegate2#init(org.eclipse.jface.action.IAction)
*/
public void init(IAction action) {
action.setChecked(!off);
configureWordWrap();
sourceButton = action;
setWrap();
action.setChecked(enabled);
}

/* (non-Javadoc)
Expand All @@ -90,34 +96,83 @@ public void setActiveEditor(IAction action, IEditorPart targetEditor) {
* @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
*/
public void run(IAction action) {
ISourceViewer viewer = getTextEditor().getViewer();
if (action.isChecked()) {
this.off = false;
setType();
cycleState();
setWrap();
action.setChecked(enabled);
}

/**
* Cycles between the three wrap states:
* off -> soft -> hard -> off ...
*
* @author Leonardo Montecchi
*/
private void cycleState() {
if (!enabled) {
enabled = true;
hard = false;
} else {
this.off = true;
TexAutoIndentStrategy.setHardWrap(false);
viewer.getTextWidget().setWordWrap(false);
if (hard) {
hard = false;
enabled = false;
} else {
hard = true;
}
}

saveWrapConfig();
}

/**
* Load wrap configuration from Eclipse properties into button state
*
* @author Leonardo Montecchi
*/
private void loadWrapConfig() {
this.enabled = TexlipsePlugin.getDefault().getPreferenceStore().getBoolean(TexlipseProperties.WORDWRAP_DEFAULT);
this.hard = TexlipsePlugin.getDefault().getPreferenceStore().getString(TexlipseProperties.WORDWRAP_TYPE) == TexlipseProperties.WORDWRAP_TYPE_HARD;

if(sourceButton != null) {
sourceButton.setChecked(enabled);
}
}

/**
* Checks the type of the word wrap and activates the correct type.
* Save wrap configuration from button state into Eclipse properties
*
* @author Leonardo Montecchi
*/
private void saveWrapConfig() {
String currentType = hard ? TexlipseProperties.WORDWRAP_TYPE_HARD : TexlipseProperties.WORDWRAP_TYPE_SOFT;
TexlipsePlugin.getDefault().getPreferenceStore().setValue(TexlipseProperties.WORDWRAP_DEFAULT, enabled);
TexlipsePlugin.getDefault().getPreferenceStore().setValue(TexlipseProperties.WORDWRAP_TYPE, currentType);
}

/**
* Activates the selected wrap mode (set editor properties).
*/
private void setType() {
String wrapStyle = TexlipsePlugin.getPreference(TexlipseProperties.WORDWRAP_TYPE);
private void setWrap() {
ISourceViewer viewer = getTextEditor() != null ? getTextEditor().getViewer() : null;
if (wrapStyle.equals(TexlipseProperties.WORDWRAP_TYPE_SOFT)) {
TexAutoIndentStrategy.setHardWrap(false);
if (viewer != null) {
viewer.getTextWidget().setWordWrap(true);
if (enabled) {
if (hard) {
TexAutoIndentStrategy.setHardWrap(true);
if (viewer != null) {
viewer.getTextWidget().setWordWrap(false);
}
} else {
TexAutoIndentStrategy.setHardWrap(false);
if (viewer != null) {
viewer.getTextWidget().setWordWrap(true);
}
}
} else if (wrapStyle.equals(TexlipseProperties.WORDWRAP_TYPE_HARD)) {
TexAutoIndentStrategy.setHardWrap(true);
} else {
TexAutoIndentStrategy.setHardWrap(false);
if (viewer != null) {
viewer.getTextWidget().setWordWrap(false);
}
}

updateIcon();
}

/**
Expand Down Expand Up @@ -152,4 +207,29 @@ public void runWithEvent(IAction action, Event event) {
run(action);
}

/**
* Updates the button icon based on current wrap state
*
* @author Leonardo Montecchi
*/
private void updateIcon() {
Bundle bundle = FrameworkUtil.getBundle(getClass());

String imgToLoad = ICON_WRAP;
String toolTip = "Word wrap *disabled*\r\nClick the button to switch mode";
if (enabled) {
if (hard) {
imgToLoad = ICON_WRAP_HARD;
toolTip = "*Hard* wrap enabled\r\nClick the button to switch mode";
} else {
imgToLoad = ICON_WRAP_SOFT;
toolTip = "*Soft* wrap enabled\r\nClick the button to switch mode";
}
}

URL imgUrl = FileLocator.find(bundle, new Path(imgToLoad),null);
ImageDescriptor updatedImage = ImageDescriptor.createFromURL(imgUrl);
sourceButton.setImageDescriptor(updatedImage);
sourceButton.setToolTipText(toolTip);
}
}

0 comments on commit 5f582bd

Please sign in to comment.