/*
* Copyright 2011 the original author or authors.
*
* Licensed 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.codehaus.groovy.eclipse.dsl.inferencing.suggestions.ui;
import java.util.HashMap;
import java.util.Map;
import org.codehaus.groovy.eclipse.dsl.inferencing.suggestions.ValueStatus;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
/**
*
* @author Nieraj Singh
* @created 2011-05-13
*/
public abstract class AbstractControlManager implements IDialogueControlManager {
private IControlSelectionListener listener;
/**
* Controls to be managed for enabling and disabling based on events
* generated by external mechanisms.
*/
private Map<Control, IDialogueControlDescriptor> controls;
private Shell shell;
/**
* By default, control values are assumed to be valid, unless this method is
* overridden by subclasses and the subclasses
* explicitly return a non-null ValueStatus with an invalid state.
*
* @param control
* @param descriptor
* @return non-null ValueStatus with an invalid state if the control value
* is invalid. Null values will be interpreted as valid status.
*/
protected ValueStatus isControlValueValid(Control control) {
return null;
}
protected void notifyControlChange(Object value, Control control) {
if (listener != null && handleInvalidCheck(control)) {
IDialogueControlDescriptor descriptor = controls.get(control);
ControlSelectionEvent event = new ControlSelectionEvent(value, descriptor);
listener.handleSelection(event);
}
}
/**
* Returns false if and only if the control exists for the given descriptor
* and indicates that the value in the widget
* is invalid. If the control does not indicate that the value is invalid,
* it will
* be assumed valid
*
* @param descriptor
* @return
*/
protected boolean handleInvalidCheck(Control control) {
if (control != null) {
ValueStatus status = isControlValueValid(control);
if (status != null && status.isError()) {
IDialogueControlDescriptor descriptor = controls.get(control);
ControlSelectionEvent event = new ControlSelectionEvent(descriptor, status.getMessage());
listener.handleInvalidSelection(event);
return false;
}
}
return true;
}
public Composite createControlArea(Composite parent) {
controls = new HashMap<Control, IDialogueControlDescriptor>();
Map<Control, IDialogueControlDescriptor> createdControls = createManagedControls(parent);
if (createdControls != null) {
controls.putAll(createdControls);
}
shell = parent.getShell();
return parent;
}
/**
* Shell where controls are shown.
*
* @return
*/
protected Shell getShell() {
return shell;
}
public void setEnabled(boolean enable) {
if (controls != null) {
for (Control control : controls.keySet()) {
if (!control.isDisposed()) {
control.setEnabled(enable);
}
}
}
}
public void addSelectionListener(IControlSelectionListener listener) {
this.listener = listener;
}
/**
* Widgets added to this map are managed in terms of disabling, and enabling
* based on events generated outside of the
* control manager.
*
* @param parent
* @return
*/
abstract protected Map<Control, IDialogueControlDescriptor> createManagedControls(Composite parent);
}