Initial commit from SVN.

This commit is contained in:
wcrisman
2014-05-30 10:31:51 -07:00
commit b45e56b890
1968 changed files with 370949 additions and 0 deletions

View File

@@ -0,0 +1,575 @@
/*
* Copyright (c) 2005,2009 Declarative Engineering LLC.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Declarative Engineering LLC
* verson 1 which accompanies this distribution, and is available at
* http://declarativeengineering.com/legal/DE_Developer_License_v1.txt
*/
package com.foundation.view.swt.custom;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import com.foundation.view.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;
/*
* A custom combo box which allows the user to setup any primary display component, and any drop display component.
*/
public abstract class CustomCombo extends Composite {
protected static final int BUTTON_PADDING = 0;
protected Control selectionControl = null;
protected Control dropControl = null;
protected Control dropButton = null;
protected Shell dropShell = null;
/** Whether the user has manually resized the drop shell and the shell should not be automatically resized when next opened. */
private boolean sizeDropShell = true;
/** Temporarily tells the listeners to ignore resize events occuring on the drop shell. */
private boolean ignoreDropShellResize = false;
/**
* CustomCombo constructor.
* @param parent A non-null widget which will be the parent of the new instance.
* @param style The style of widget to construct.
* @see SWT#RESIZE
* @see SWT#BORDER
* @see Widget#getStyle
*/
public CustomCombo(Composite parent, int style) {
super(parent, (style | SWT.NO_BACKGROUND | SWT.INHERIT_DEFAULT) & (~SWT.RESIZE));
int buttonStyle = SWT.ARROW | SWT.DROP_DOWN | SWT.MAX;
int dropShellStyle = SWT.NO_BACKGROUND | SWT.TOOL | SWT.ON_TOP | SWT.NO_FOCUS;
TraverseListener traverseListener = new TraverseListener() {
public void keyTraversed(TraverseEvent event) {
switch(event.detail) {
case SWT.TRAVERSE_RETURN:
case SWT.TRAVERSE_TAB_PREVIOUS:
case SWT.TRAVERSE_TAB_NEXT:
case SWT.TRAVERSE_ARROW_PREVIOUS:
case SWT.TRAVERSE_PAGE_PREVIOUS:
case SWT.TRAVERSE_PAGE_NEXT:
if(isShowingDrop()) {
hideDrop(true);
event.doit = true;
}//if//
break;
case SWT.TRAVERSE_ESCAPE:
if(isShowingDrop()) {
hideDrop(false);
event.doit = false;
}//if//
break;
default:
break;
}//switch//
}//keyTraversed()//
};
//TODO: Do any native systems put the drop button on the left?
super.setLayout(new Layout() {
protected void layout(Composite composite, boolean flushCache) {
internalLayout(composite, flushCache);
}//layout()//
protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) {
return internalComputeSize(composite, wHint, hHint, flushCache);
}//computeSize()//
});
if((style & SWT.RESIZE) > 0) {
dropShellStyle |= SWT.RESIZE | SWT.BORDER;
}//if//
dropButton = createDropButton();
//Add a listener to this composite to allow resizing of the children.//
addControlListener(new ControlListener() {
public void controlMoved(ControlEvent event) {
}//controlMoved()//
public void controlResized(ControlEvent event) {
/* TODO Remove
Rectangle area = getClientArea();
arrow.setBounds(area.width - arrowSize.x, 0, arrowSize.x, area.height);
valueAreaBounds = new Rectangle(0, 0, area.width - arrowSize.x, area.height);
*/
}//controlResized()//
});
//Add a listener to this composite to hide the drop down shell if a traveral event occurs.//
addTraverseListener(traverseListener);
//Add a listener to this composite to redraw after a focus change.//
addFocusListener(new FocusListener() {
public void focusLost(FocusEvent event) {
redraw();
}//focusLost()//
public void focusGained(FocusEvent event) {
redraw();
}//focusGained()//
});
//Add a listener to this composite to show the drop down if a mouse click occurs outside the children but inside the composite.//
addMouseListener(new MouseListener() {
public void mouseDoubleClick(MouseEvent event) {
}//mouseDoubleClick()//
public void mouseDown(MouseEvent event) {
if(!isShowingDrop()) {
showDrop();
forceFocus();
}//if//
}//mouseDown()//
public void mouseUp(MouseEvent event) {
}//mouseUp()//
});
dropShell = new Shell(getShell(), dropShellStyle);
dropShell.setLayout(new FillLayout());
dropShell.setBackgroundMode(SWT.INHERIT_FORCE);
dropShell.addControlListener(new ControlListener() {
public void controlResized(ControlEvent event) {
if(!ignoreDropShellResize) {
//Keep the combo from resizing the drop down shell when it next opens.//
sizeDropShell = false;
}//if//
}//controlResized()//
public void controlMoved(ControlEvent event) {
}//controlMoved()//
});
dropShell.addShellListener(new ShellListener() {
public void shellActivated(ShellEvent event) {
}//shellActivated()//
public void shellClosed(ShellEvent event) {
}//shellClosed()//
public void shellDeactivated(ShellEvent event) {
hideDrop(true);
}//shellDeactivated()//
public void shellDeiconified(ShellEvent event) {
}//shellDeiconified()//
public void shellIconified(ShellEvent event) {
}//shellIconified()//
});
dropShell.addTraverseListener(traverseListener);
getDisplay().addFilter(SWT.MouseDown, new Listener() {
public void handleEvent(Event event) {
if((isShowingDrop()) && (event.widget instanceof Control)) {
Control c = (Control) event.widget;
//If the mouse event occured outside the drop shell then close the drop shell.//
if(!dropShell.getBounds().contains(getDisplay().map(c, null, event.x, event.y))) {
hideDrop(true);
event.type = 0;
}//if//
}//if//
}//handleEvent//
});
//if the outer most shell is deactivated or iconified, then stop showing the drop down shell.//
getShell().addShellListener(new ShellListener() {
public void shellActivated(ShellEvent event) {
}//shellActivated()//
public void shellClosed(ShellEvent event) {
}//shellClosed()//
public void shellDeactivated(ShellEvent event) {
if(!isDisposed()) {
getShell().getDisplay().asyncExec(new Runnable() {
public void run() {
//TODO: Clean this up so it works properly.
if((isShowingDrop()) && (dropShell != getDisplay().getActiveShell())) {
getShell().setActive();
hideDrop(true);
}//if//
}//run()//
});
}//if//
}//shellDeactivated()//
public void shellDeiconified(ShellEvent event) {
}//shellDeiconified()//
public void shellIconified(ShellEvent event) {
if(isShowingDrop()) {
hideDrop(false);
}//if//
}//shellIconified()//
});
getShell().addControlListener(new ControlListener() {
public void controlMoved(ControlEvent event) {
if(isShowingDrop()) {
hideDrop(false);
}//if//
}//controlMoved()//
public void controlResized(ControlEvent event) {
if(isShowingDrop()) {
hideDrop(false);
}//if//
}//controlResized()//
});
initialize();
dropControl = createDropControl(dropShell);
selectionControl = createSelectionControl();
postInitialize();
}//CustomCombo()//
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Control#getBackground()
*/
public Color getBackground() {
return selectionControl.getBackground();
}//getBackground()//
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Control#setBackground(org.eclipse.swt.graphics.Color)
*/
public void setBackground(Color color) {
super.setBackground(color);
//selectionControl.setBackground(color);
//dropButton.setBackground(color);
dropControl.setBackground(color);
}//setBackground()//
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Control#getBackgroundImage()
*/
public Image getBackgroundImage() {
return selectionControl.getBackgroundImage();
}//getBackgroundImage()//
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Control#setBackgroundImage(org.eclipse.swt.graphics.Image)
*/
public void setBackgroundImage(Image image) {
super.setBackgroundImage(image);
//selectionControl.setBackgroundImage(image);
//dropButton.setBackgroundImage(image);
dropControl.setBackgroundImage(image);
}//setBackgroundImage()//
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Control#getFont()
*/
public Font getFont() {
return selectionControl.getFont();
}//getFont()//
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Control#setFont(org.eclipse.swt.graphics.Font)
*/
public void setFont(Font font) {
super.setFont(font);
selectionControl.setFont(font);
dropButton.setFont(font);
dropControl.setFont(font);
}//setFont()//
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Control#getForeground()
*/
public Color getForeground() {
return selectionControl.getForeground();
}//getForeground()//
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Control#setForeground(org.eclipse.swt.graphics.Color)
*/
public void setForeground(Color color) {
super.setForeground(color);
selectionControl.setForeground(color);
dropButton.setForeground(color);
dropControl.setForeground(color);
}//setForeground()//
/**
* Initializes the combo's state prior to any of the controls being setup.
*/
protected abstract void initialize();
/**
* Initializes the combo's state after all the controls have been setup.
*/
protected abstract void postInitialize();
/**
* Creates the control for displaying the selection.
* @return The control that displays the current drop down selection or contents.
*/
protected abstract Control createSelectionControl();
/**
* Creates the control displayed in the drop down.
* @return The drop down contents.
*/
protected abstract Control createDropControl(Shell parent);
/**
* Creates a new drop button control.
* @return The button used to open the drop shell.
*/
protected Control createDropButton() {
Button dropButton = new Button(this, SWT.ARROW | SWT.DOWN | SWT.FLAT | SWT.INHERIT_DEFAULT);
dropButton.addMouseListener(new MouseListener() {
public void mouseDoubleClick(MouseEvent event) {
}//mouseDoubleClick()//
public void mouseDown(MouseEvent event) {
if(!isShowingDrop()) {
showDrop();
}//if//
}//mouseDown()//
public void mouseUp(MouseEvent event) {
}//mouseUp()//
});
dropButton.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent event) {
forceFocus();
}//widgetSelected()//
public void widgetDefaultSelected(SelectionEvent event) {
}//widgetDefaultSelected()//
});
return dropButton;
}//createDropButton()//
/**
* Gets the control that displays the combo selection.
* @return The control to be used to display the selection.
*/
protected Control getSelectionControl() {
return selectionControl;
}//getSelectionControl()//
/**
* Gets the control that displays the drop down.
* @return The control to be used to display the drop down.
*/
protected Control getDropControl() {
return dropControl;
}//getDropControl()//
/**
* Gets the control used to open the drop panel.
* @return The button control whose action opens the drop panel.
*/
protected Control getDropButton() {
return dropButton;
}//getDropButton()//
/**
* Gets the shell that contains the drop panel.
* @return The shell that is used to display the drop panel.
*/
protected Shell getDropShell() {
return dropShell;
}//getDropShell()//
/**
* Determines whether the drop down shell is currently being shown.
* @return Whether we are currently showing the drop control.
*/
public boolean isShowingDrop() {
return dropShell != null && !dropShell.isDisposed() && dropShell.isVisible();
}//isShowingDrop()//
/**
* Shows the drop down shell.
*/
public final void showDrop() {
checkWidget();
if(!isShowingDrop()) {
if(!dropShell.isDisposed()) {
Point comboSize = getSize();
Point size;
Point location;
refreshDrop();
//Get the combo location within the screen.//
location = getDisplay().map(getParent(), null, getLocation());
if(sizeDropShell) {
int trimWidth = dropShell.computeTrim(0, 0, 1, 1).width - 1;
Rectangle bounds;
int quarterScreenHeight = getDisplay().getClientArea().height / 4;
size = dropShell.computeSize(comboSize.x - trimWidth, -1);
if(size.y > quarterScreenHeight) {
size.y = quarterScreenHeight;
}//if//
bounds = dropShell.computeTrim(0, 0, size.x, size.y);
dropShell.setSize(bounds.width, bounds.height);
}//if//
else {
size = dropShell.getSize();
}//else//
//Horizontal positioning.//
if(size.x < comboSize.x) {
location.x += comboSize.x - size.x;
}//if//
else {
location.x -= (size.x - comboSize.x) >> 1;
}//else//
//Horizontal bounds checking.//
if(location.x + size.x > getDisplay().getClientArea().width) {
location.x = getDisplay().getClientArea().width - size.x;
}//if//
if(location.x < getDisplay().getClientArea().x) {
location.x = getDisplay().getClientArea().x;
}//if//
//Vertical positioning.//
location.y += comboSize.y;
//Make the pull down go up instead of down.//
if(location.y + size.y > getDisplay().getClientArea().height) {
location.y -= comboSize.y + size.y;
}//if//
if(location.y < getDisplay().getClientArea().y) {
//Use the larger of the two positionings and then resize the pulldown.//
if(location.y < (location.y + comboSize.y + size.y + size.y) - getDisplay().getClientArea().height) {
//Make the combo drop down again since it will have the most space this way.//
location.y += comboSize.y + size.y;
size.y -= getDisplay().getClientArea().height - (location.y + size.y);
}//if//
else {
size.y += location.y;
location.y = getDisplay().getClientArea().y;
}//else//
//Adjust the shell size.//
dropShell.setSize(size);
}//if//
ignoreDropShellResize = true;
dropShell.setLocation(location);
dropShell.setVisible(true);
ignoreDropShellResize = false;
redraw();
}//if//
}//if//
}//showDrop()//
/**
* Hides the drop down shell.
* @param synchronize Whether the drop control should synchronize its data with the combo.
*/
public final void hideDrop(boolean synchronize) {
checkWidget();
if(isShowingDrop()) {
dropShell.setVisible(false);
getShell().setActive();
forceFocus();
forceFocus();
redraw();
if(synchronize) {
synchronizeDrop();
}//if//
}//if//
}//hideDrop()//
/**
* Rebuilds the drop control.
*/
protected void rebuildDropControl() {
dropControl.dispose();
dropControl = createDropControl(dropShell);
if(isShowingDrop()) {
dropShell.layout(true, true);
dropShell.redraw();
}//if//
}//rebuildDropControl()//
/**
* Rebuilds the selection control.
*/
protected void rebuildSelectionControl() {
selectionControl.dispose();
selectionControl = createSelectionControl();
layout(true, true);
redraw();
}//rebuildSelectionControl()//
/**
* Called to refresh the drop control's data.
*/
protected abstract void refreshDrop();
/**
* Called to synchronize the drop control's data.
*/
protected abstract void synchronizeDrop();
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Composite#setLayout(org.eclipse.swt.widgets.Layout)
*/
public void setLayout(Layout layout) {
}//setLayout()//
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Control#computeSize(int, int, boolean)
*/
public Point computeSize(int wHint, int hHint, boolean changed) {
Point buttonSize = getDropButton().computeSize(-1, hHint, changed);
Point size = getSelectionControl().computeSize(-1, hHint, changed);
Point dropSize = getDropShell().computeSize(-1, -1, changed);
size.y = Math.max(buttonSize.y, size.y);
size.x += buttonSize.x;
size.x = Math.max(size.x, dropSize.x);
return size;
}//computeSize()//
/**
* Called by the container's layout code.
* @param composite The composite to be layed out.
* @param flushCache Whether the cache of layout data must be flushed.
*/
protected void internalLayout(Composite composite, boolean flushCache) {
Point size = composite.getSize();
Point buttonSize = getDropButton().computeSize(-1, size.y, flushCache);
Point selectionSize = new Point(0, size.y);
buttonSize.y = size.y;
getDropButton().setLocation(size.x - buttonSize.x + getButtonOffset(), getButtonOffset());
buttonSize.y += getButtonOffset();
buttonSize.x += getButtonOffset();
getDropButton().setSize(buttonSize);
selectionSize.x = size.x - buttonSize.x;
getSelectionControl().setSize(selectionSize);
getSelectionControl().setLocation(0, 0);
}//internalLayout()//
/**
* Gets the offset to use when laying out the drop button.
* <p>This is required by windows which will not display the button correctly without the offset. This may need to be platform specific.</p>
* @return The offset to shift the button down and right (or if negative up and left).
*/
protected int getButtonOffset() {
return SWT.getPlatform().equals("win32") ? -1 : 0;
}//getButtonOffset()//
/**
* Called by the container's layout code.
* @param composite The composite whose size is being computed.
* @param wHint The width hint.
* @param hHint The height hint.
* @param flushCache Whether the cache of layout data must be flushed.
* @return The component's size.
*/
protected Point internalComputeSize(Composite composite, int wHint, int hHint, boolean flushCache) {
Point selectionSize = getSelectionControl().computeSize(wHint, hHint, flushCache);
Point dropButtonSize = getDropButton().computeSize(-1, hHint, flushCache);
selectionSize.x += dropButtonSize.x;
selectionSize.y = Math.max(selectionSize.y, dropButtonSize.y);
return selectionSize;
}//internalComputeSize()//
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Widget#dispose()
*/
public void dispose() {
if(dropControl != null && !dropControl.isDisposed()) {
dropControl.dispose();
dropControl = null;
}//if//
if(dropShell != null && !dropShell.isDisposed()) {
dropShell.dispose();
dropShell = null;
}//if//
if(dropButton != null && !dropButton.isDisposed()) {
dropButton.dispose();
dropButton = null;
}//if//
if(selectionControl != null && !selectionControl.isDisposed()) {
selectionControl.dispose();
selectionControl = null;
}//if//
super.dispose();
}//dispose()//
}//CustomCombo//