|
|
|
Re: Supporting disabled buttons and toggle buttons on the toolbar [message #1072777 is a reply to message #1065123] |
Tue, 23 July 2013 13:25 |
Eclipse User |
|
|
|
Thank you all for the input.
The problem here is again the static workbench model of Eclipse 3.x. We have to find somehow a way to propagate dynamic to the static workbench model. With e4 everything will be better or at least different .
So for now you are able to change/add a few classes to get toggle tool buttons.
- Change your 'whatEverName'.ui.swt.application.button.CoolbarButton class to:
package ch.ahoegger.sessiontest.ui.swt.application.button;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.scout.commons.OptimisticLock;
import org.eclipse.scout.commons.logger.IScoutLogger;
import org.eclipse.scout.commons.logger.ScoutLogManager;
import org.eclipse.scout.rt.client.ui.action.IAction;
import org.eclipse.scout.rt.client.ui.action.keystroke.KeyStroke;
import org.eclipse.scout.rt.ui.swt.ISwtEnvironment;
import org.eclipse.scout.rt.ui.swt.util.SwtUtility;
import org.eclipse.swt.SWT;
public class CoolbarButton extends Action {
private static final IScoutLogger LOG = ScoutLogManager.getLogger(CoolbarButton.class);
private IAction m_scoutAction;
private ISwtEnvironment m_swtEnvironment;
private final OptimisticLock m_updateSwtFromScoutLock;
private boolean m_updateUi = true;
private P_ScoutPropertyChangeListener m_scoutPropertyListener;
private ToolBarManager m_toolbarMananger;
public CoolbarButton(IAction scoutAction, ToolBarManager manager, ISwtEnvironment environment) {
super((scoutAction.getText() == null) ? (" ") : scoutAction.getText(), transformScoutStyle(scoutAction));
m_toolbarMananger = manager;
m_swtEnvironment = environment;
m_updateSwtFromScoutLock = new OptimisticLock();
m_scoutAction = scoutAction;
setId(getScoutObject().getActionId());
attachScout();
m_scoutAction.addPropertyChangeListener(new P_ScoutPropertyChangeListener());
}
private static int transformScoutStyle(IAction scoutAction) {
if (scoutAction.isToggleAction()) {
return SWT.TOGGLE;
}
return AS_PUSH_BUTTON;
}
protected void attachScout() {
try {
setUpdateUi(false);
updateEnabledFromScout();
updateIconFromScout();
updateKeystrokeFromScout();
updateSelectedFromScout();
updateTextFromScout();
updateTooltipTextFromScout();
}
finally {
setUpdateUi(true);
}
}
/**
* @return the swtEnvironment
*/
public ISwtEnvironment getEnvironment() {
return m_swtEnvironment;
}
protected IAction getScoutObject() {
return m_scoutAction;
}
/**
* @param updateUi
* the updateUi to set
*/
public void setUpdateUi(boolean updateUi) {
if (updateUi != m_updateUi) {
m_updateUi = updateUi;
if (updateUi) {
m_toolbarMananger.update(true);
}
}
}
/**
* @return the updateUi
*/
public boolean isUpdateUi() {
return m_updateUi;
}
protected void updateEnabledFromScout() {
setEnabled(getScoutObject().isEnabled());
if (isUpdateUi()) {
m_toolbarMananger.update(true);
}
}
protected void updateIconFromScout() {
setImageDescriptor(getEnvironment().getImageDescriptor(getScoutObject().getIconId()));
if (isUpdateUi()) {
m_toolbarMananger.update(true);
}
}
protected void updateKeystrokeFromScout() {
String keyStroke = getScoutObject().getKeyStroke();
if (keyStroke != null) {
int keyCode = SwtUtility.getSwtKeyCode(new KeyStroke(keyStroke));
int stateMask = SwtUtility.getSwtStateMask(new KeyStroke(keyStroke));
setAccelerator(stateMask | keyCode);
}
else {
setAccelerator(SWT.NONE);
}
if (isUpdateUi()) {
m_toolbarMananger.update(true);
}
}
protected void updateTextFromScout() {
setText(getScoutObject().getText());
if (isUpdateUi()) {
m_toolbarMananger.update(true);
}
}
protected void updateTooltipTextFromScout() {
setToolTipText(getScoutObject().getTooltipText());
if (isUpdateUi()) {
m_toolbarMananger.update(true);
}
}
protected void updateSelectedFromScout() {
setChecked(getScoutObject().isSelected());
if (isUpdateUi()) {
m_toolbarMananger.update(true);
}
}
protected void updateVisibleFromScout() {
LOG.warn("set visible on SWT action is not supported");
}
@Override
public void run() {
handleSwtAction();
}
protected void handleSwtAction() {
try {
if (getUpdateSwtFromScoutLock().acquire()) {
Runnable t = new Runnable() {
@Override
public void run() {
if (getScoutObject().isToggleAction()) {
getScoutObject().getUIFacade().setSelectedFromUI(!getScoutObject().isSelected());
}
else {
getScoutObject().getUIFacade().fireActionFromUI();
}
}
};
getEnvironment().invokeScoutLater(t, 0);
}
}
finally {
getUpdateSwtFromScoutLock().release();
}
}
/**
* @return the lock used in the Swt thread when applying scout changes
*/
public OptimisticLock getUpdateSwtFromScoutLock() {
return m_updateSwtFromScoutLock;
}
protected void handleScoutPropertyChange(String propertyName, Object newValue) {
if (IAction.PROP_ENABLED.equals(propertyName)) {
updateEnabledFromScout();
}
else if (IAction.PROP_ICON_ID.equals(propertyName)) {
updateIconFromScout();
}
else if (IAction.PROP_KEYSTROKE.equals(propertyName)) {
updateKeystrokeFromScout();
}
else if (IAction.PROP_SELECTED.equals(propertyName)) {
updateSelectedFromScout();
}
else if (IAction.PROP_TEXT.equals(propertyName)) {
updateTextFromScout();
}
else if (IAction.PROP_TOOLTIP_TEXT.equals(propertyName)) {
updateTooltipTextFromScout();
}
else if (IAction.PROP_VISIBLE.equals(propertyName)) {
updateVisibleFromScout();
}
}
private class P_ScoutPropertyChangeListener implements PropertyChangeListener {
@Override
public void propertyChange(final PropertyChangeEvent evt) {
Runnable t = new Runnable() {
@Override
public void run() {
try {
getUpdateSwtFromScoutLock().acquire();
//
handleScoutPropertyChange(evt.getPropertyName(), evt.getNewValue());
}
finally {
getUpdateSwtFromScoutLock().release();
}
}
};
getEnvironment().invokeSwtLater(t);
}
}
}
- Change your 'whatEverName'.ui.swt.application.ApplicationActionBarAdvisor to:
package ch.ahoegger.sessiontest.ui.swt.application;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.ICoolBarManager;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.ToolBarContributionItem;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.scout.commons.logger.IScoutLogger;
import org.eclipse.scout.commons.logger.ScoutLogManager;
import org.eclipse.scout.rt.client.ui.action.tool.IToolButton;
import org.eclipse.scout.rt.client.ui.action.view.IViewButton;
import org.eclipse.scout.rt.client.ui.desktop.IDesktop;
import org.eclipse.scout.rt.ui.swt.ISwtEnvironment;
import org.eclipse.swt.SWT;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import ch.ahoegger.sessiontest.ui.swt.Activator;
import ch.ahoegger.sessiontest.ui.swt.SwtEnvironment;
import ch.ahoegger.sessiontest.ui.swt.application.button.CoolbarButton;
/**
* <h3>ApplicationActionBarAdvisor</h3> Used for menu contributions.
*/
public class ApplicationActionBarAdvisor extends ActionBarAdvisor {
private static IScoutLogger logger = ScoutLogManager.getLogger(ApplicationActionBarAdvisor.class);
private ToolBarManager m_toolbar;
public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
super(configurer);
((SwtEnvironment) Activator.getDefault().getEnvironment()).setAdvisor(this);
}
@Override
protected void fillMenuBar(IMenuManager menuBar) {
menuBar.add(new MenuManager("", IWorkbenchActionConstants.M_FILE));
}
public void initViewButtons(ISwtEnvironment swtEnvironment, IDesktop d) {
for (IToolButton scoutToolButton : d.getToolButtons()) {
if (scoutToolButton.isVisible() && scoutToolButton.isVisibleGranted()) {
CoolbarButton swtAction = new CoolbarButton(scoutToolButton, m_toolbar, swtEnvironment);
contributeToCoolBar(swtAction);
}
}
for (IViewButton scoutViewButton : d.getViewButtons()) {
if (scoutViewButton.isVisible() && scoutViewButton.isVisibleGranted()) {
CoolbarButton swtAction = new CoolbarButton(scoutViewButton, m_toolbar, swtEnvironment);
contributeToCoolBar(swtAction);
}
}
}
protected void contributeToCoolBar(Action swtAction) {
ActionContributionItem contributionItem = new ActionContributionItem(swtAction);
contributionItem.setMode(ActionContributionItem.MODE_FORCE_TEXT);
m_toolbar.add(contributionItem);
}
@Override
protected void fillCoolBar(ICoolBarManager coolBar) {
m_toolbar = new ToolBarManager(SWT.FLAT | SWT.RIGHT);
ToolBarContributionItem item = new ToolBarContributionItem(m_toolbar, "main");
coolBar.add(item);
}
}
- Ensure in 'whatEverName'.ui.swt.application.ApplicationWorkbenchWindowAdvisor.preWindowOpen() the configurer.setShowCoolBar(true); is set.
The final solution for toolbar button issues is scheduled for 3.10 (Luna) release and hopefully be part of the framework;). I opened Bugzilla 413524.
-andreas
|
|
|
Re: Supporting disabled buttons and toggle buttons on the toolbar [message #1073247 is a reply to message #1072777] |
Wed, 24 July 2013 11:42 |
Urs Beeli Messages: 573 Registered: October 2012 Location: Bern, Switzerland |
Senior Member |
|
|
Thanks. As far as I can tell, this works well for AbstractToolButton and AbstracFormToolButton. However, I have trouble with AbstractOutlineViewButtons. When I click on them, they change toggle state but the execAction() method is not called, hence the outline is not changed.
Also, in ApplicationWorkbenchAdvisor.initialize we have the following line of code to allow saving of window/view size/position:
configurer.setSaveAndRestore(true);
This leads to the Coolbar being created when the Workbench is set up, long before Scout is ready. In order to make sure the CoolBar is really shown, I've had to add the following code to ApplicationActionBarAdvisor:
A member variable to store the configurer:
private IActionBarConfigurer m_configurer;
In the constructor, I store the configurer:
m_configurer = configurer;
Then at the end of initViewButtons I need the following code:
m_configurer.getCoolBarManager().update(true);
Using the above, the CoolBar is shown even when setSaveAndRestore is active.
|
|
|
|
|
Re: Supporting disabled buttons and toggle buttons on the toolbar [message #1073629 is a reply to message #1073258] |
Thu, 25 July 2013 07:51 |
Eclipse User |
|
|
|
you might probably also change the CoolbarButton.handleSwtAction to :
protected void handleSwtAction() {
try {
if (getUpdateSwtFromScoutLock().acquire()) {
Runnable t = new Runnable() {
@Override
public void run() {
if (getScoutObject().isToggleAction()) {
getScoutObject().getUIFacade().setSelectedFromUI(!getScoutObject().isSelected());
}
getScoutObject().getUIFacade().fireActionFromUI();
}
};
getEnvironment().invokeScoutLater(t, 0);
}
}
finally {
getUpdateSwtFromScoutLock().release();
}
}
Let me know if that works...
-andreas
|
|
|
|
Powered by
FUDForum. Page generated in 0.03928 seconds