private Window window = null;
public void startup(final Display display, Dictionary<String, String> properties) throws Exception {
// pivot.wtk.Theme.setTheme(new pivot.wtk.skin.terra.TerraTheme("test"));
WTKXSerializer wtkxSerializer = new WTKXSerializer();
Component content = (Component)wtkxSerializer.readObject("pivot/tutorials/demo.wtkx");
// Text
PlainTextSerializer plainTextSerializer = new PlainTextSerializer("UTF-8");
InputStream inputStream = getClass().getResourceAsStream("text_area.txt");
Document document = null;
try {
document = plainTextSerializer.readObject(inputStream);
} catch(Exception exception) {
System.out.println(exception);
}
TextArea textArea = (TextArea)wtkxSerializer.getObjectByName("text.textArea");
textArea.setDocument(document);
final WatermarkDecorator watermarkDecorator = new WatermarkDecorator("Preview");
watermarkDecorator.setOpacity(0.1f);
watermarkDecorator.setFont(watermarkDecorator.getFont().deriveFont(Font.BOLD, 24));
textArea.getDecorators().add(watermarkDecorator);
textArea.getComponentStateListeners().add(new ComponentStateListener() {
public void enabledChanged(Component component) {
// No-op
}
public void focusedChanged(Component component, boolean temporary) {
component.getDecorators().remove(watermarkDecorator);
component.getComponentStateListeners().remove(this);
}
});
new Action("selectImageAction") {
public String getDescription() {
return "Select Image Action";
}
public void perform() {
Button.Group imageMenuGroup = Button.getGroup("imageMenuGroup");
Button selectedItem = imageMenuGroup.getSelection();
String imageName = (String)selectedItem.getUserData();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL imageURL = classLoader.getResource(imageName);
// If the image has not been added to the resource cache yet,
// add it
Image image = (Image)ApplicationContext.getResourceCache().get(imageURL);
if (image == null) {
image = Image.load(imageURL);
ApplicationContext.getResourceCache().put(imageURL, image);
}
// Update the image
menuImageView.setImage(image);
}
};
ListView iconListView = (ListView)wtkxSerializer.getObjectByName("lists.iconListView");
iconListView.setItemDisabled(3, true);
iconListView.setItemDisabled(4, true);
ListView checkedListView = (ListView)wtkxSerializer.getObjectByName("lists.checkedListView");
checkedListView.setItemChecked(0, true);
checkedListView.setItemChecked(2, true);
checkedListView.setItemChecked(3, true);
menuImageView = (ImageView)wtkxSerializer.getObjectByName("menus.imageView");
menuImageView.getComponentMouseButtonListeners().add(new ComponentMouseButtonListener() {
public boolean mouseDown(Component component, Mouse.Button button, int x, int y) {
if (button == Mouse.Button.RIGHT) {
menuPopup.open(display, component.mapPointToAncestor(display, x, y));
}
return false;
}
public boolean mouseUp(Component component, Mouse.Button button, int x, int y) {
return false;
}
public boolean mouseClick(Component component, Mouse.Button button, int x, int y, int count) {
return false;
}
});
// Spinners
Spinner numericSpinner = (Spinner)wtkxSerializer.getObjectByName("spinners.numericSpinner");
initializeNumericSpinner(numericSpinner);
Spinner dateSpinner = (Spinner)wtkxSerializer.getObjectByName("spinners.dateSpinner");
initializeDateSpinner(dateSpinner);
// Sliders
SliderValueListener sliderValueListener = new SliderValueListener() {
public void valueChanged(Slider slider, int previousValue) {
Color color = new Color(redSlider.getValue(), greenSlider.getValue(),
blueSlider.getValue());
colorBorder.getStyles().put("backgroundColor", color);
}
};
redSlider = (Slider)wtkxSerializer.getObjectByName("spinners.redSlider");
redSlider.getSliderValueListeners().add(sliderValueListener);
greenSlider = (Slider)wtkxSerializer.getObjectByName("spinners.greenSlider");
greenSlider.getSliderValueListeners().add(sliderValueListener);
blueSlider = (Slider)wtkxSerializer.getObjectByName("spinners.blueSlider");
blueSlider.getSliderValueListeners().add(sliderValueListener);
Color color = new Color(redSlider.getValue(), greenSlider.getValue(),
blueSlider.getValue());
colorBorder = (Border)wtkxSerializer.getObjectByName("spinners.colorBorder");
colorBorder.getStyles().put("backgroundColor", color);
sortableTableView = (TableView)wtkxSerializer.getObjectByName("tables.sortableTableView");
sortableTableViewHeader = (TableViewHeader)wtkxSerializer.getObjectByName("tables.sortableTableViewHeader");
customTableView = (TableView)wtkxSerializer.getObjectByName("tables.customTableView");
initializeTableViews();
DragSource imageDragSource = new DragSource() {
private Image image = null;
private Point offset = null;
private LocalManifest content = null;
public boolean beginDrag(Component component, int x, int y) {
ImageView imageView = (ImageView)component;
image = imageView.getImage();
if (image != null) {
imageView.setImage((Image)null);
content = new LocalManifest();
content.putImage(image);
offset = new Point(x - (imageView.getWidth() - image.getWidth()) / 2,
y - (imageView.getHeight() - image.getHeight()) / 2);
}
return (image != null);
}
public void endDrag(Component component, DropAction dropAction) {
if (dropAction == null) {
ImageView imageView = (ImageView)component;
imageView.setImage(image);
}
image = null;
offset = null;
content = null;
}
public boolean isNative() {
return false;
}
public LocalManifest getContent() {
return content;
}
public Visual getRepresentation() {
return image;
}
public Point getOffset() {
return offset;
}
public int getSupportedDropActions() {
return DropAction.MOVE.getMask();
}
};
DropTarget imageDropTarget = new DropTarget() {
public DropAction dragEnter(Component component, Manifest dragContent,
int supportedDropActions, DropAction userDropAction) {
DropAction dropAction = null;
ImageView imageView = (ImageView)component;
if (imageView.getImage() == null
&& dragContent.containsImage()
&& DropAction.MOVE.isSelected(supportedDropActions)) {
dropAction = DropAction.MOVE;
component.getStyles().put("backgroundColor", "#f0e68c");
}
return dropAction;
}
public void dragExit(Component component) {
component.getStyles().put("backgroundColor", null);
}
public DropAction dragMove(Component component, Manifest dragContent,
int supportedDropActions, int x, int y, DropAction userDropAction) {
return (dragContent.containsImage() ? DropAction.MOVE : null);
}
public DropAction userDropActionChange(Component component, Manifest dragContent,
int supportedDropActions, int x, int y, DropAction userDropAction) {
return (dragContent.containsImage() ? DropAction.MOVE : null);
}
public DropAction drop(Component component, Manifest dragContent,
int supportedDropActions, int x, int y, DropAction userDropAction) {
DropAction dropAction = null;
if (dragContent.containsImage()) {
ImageView imageView = (ImageView)component;
try {
imageView.setImage(dragContent.getImage());
dropAction = DropAction.MOVE;
} catch(IOException exception) {
System.err.println(exception);
}
}
dragExit(component);
return dropAction;
}
};
ImageView imageView1 = (ImageView)wtkxSerializer.getObjectByName("dragdrop.imageView1");
imageView1.setDragSource(imageDragSource);
imageView1.setDropTarget(imageDropTarget);
ImageView imageView2 = (ImageView)wtkxSerializer.getObjectByName("dragdrop.imageView2");
imageView2.setDragSource(imageDragSource);
imageView2.setDropTarget(imageDropTarget);
ImageView imageView3 = (ImageView)wtkxSerializer.getObjectByName("dragdrop.imageView3");
imageView3.setDragSource(imageDragSource);
imageView3.setDropTarget(imageDropTarget);
alertButton = (PushButton)wtkxSerializer.getObjectByName("alerts.alertButton");
promptButton = (PushButton)wtkxSerializer.getObjectByName("alerts.promptButton");
initializeAlertButtons();
menuPopup = new MenuPopup((Menu)wtkxSerializer.readObject("pivot/tutorials/menu_popup.wtkx"));
window = new Window();
window.setTitle("Pivot Demo");
window.setMaximized(true);
window.setContent(content);