package org.rsg.interfascia_v2;
import org.rsg.interfascia_v2.Constants;
import org.rsg.interfascia_v2.components.Stack4Components;
import java.awt.Toolkit;
import java.awt.datatransfer.*;
import java.awt.event.KeyEvent;
import java.io.IOException;
import processing.core.*;
public class Controller implements ClipboardOwner {
//private GUIComponent contents[];
private Stack4Components contents;
//private int numItems;
private int focusIndex;
private boolean visible;
private LookAndFeel lookAndFeel;
//public PGraphicsState userState;
private Clipboard clipboard;
public PApplet papplet;
public boolean showBounds;
public String name = "";
public Controller(String s, PApplet newParent) {
this(s, newParent, true);
}
public Controller(String s, PApplet newParent, boolean newVisible) {
//numItems = 0;
name = s;
focusIndex = -1;
showBounds = false;
setParent(newParent);
setVisible(newVisible);
contents = new Stack4Components();
lookAndFeel = new LookAndFeel(papplet, Constants.LOOK_AND_FEEL_TYPE);
//userState = new PGraphicsState();
SecurityManager security = System.getSecurityManager();
if (security != null)
try {
security.checkSystemClipboardAccess();
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
} catch (SecurityException e) {
clipboard = new Clipboard("Interfascia Clipboard");
}
else
try {
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
} catch (Exception exception) {
}
papplet.registerKeyEvent(this);
//applet.registerDraw(this);
}
public void setLookAndFeel(LookAndFeel lf) {
lookAndFeel = lf;
}
public LookAndFeel getLookAndFeel() {
return lookAndFeel;
}
public void add(Component c) {
/*if(numItems == contents.length)
{
GUIComponent temp[] = contents;
contents = new GUIComponent[contents.length * 2];
System.arraycopy(temp, 0, contents, 0, numItems);
}*/
c.setController(this);
c.setLookAndFeel(lookAndFeel);
//contents[numItems++] = component;
c.initWithParent();
contents.add(c);
}
public void removeAll() {
//remove(component);
contents.clear();
//contents = new GUIComponent[1];
}
public void remove(Component c) {
contents.remove(c);
/*int componentIndex = -1;
for(int i = 0; i < numItems; i++)
{
if(component != contents[i])
continue;
componentIndex = i;
break;
}
if(componentIndex != -1)
{
contents[componentIndex] = null;
if(componentIndex < numItems - 1)
System.arraycopy(contents, componentIndex + 1, contents, componentIndex, numItems);
numItems--;
}*/
}
public void setParent(PApplet ilan) {
this.papplet = ilan;
}
public PApplet getParent() {
return papplet;
}
public void setVisible(boolean newVisible) {
visible = newVisible;
}
public boolean getVisible() {
return visible;
}
public void requestFocus(Component c) {
for (int i = 0; i < size(); i++)
if (c == contents.get(i))
focusIndex = i;
}
public void yieldFocus(Component c) {
if (focusIndex > -1 && focusIndex < size()
&& getComponentWithFocus() == c)
focusIndex = -1;
}
public Component getComponentWithFocus() {
return contents.get(focusIndex);
}
public boolean getFocusStatusForComponent(Component c) {
if (focusIndex >= 0 && focusIndex < size())
return c == getComponentWithFocus();
else
return false;
}
public void lostOwnership(Clipboard parClipboard,
Transferable parTransferable) {
System.out.println("Lost ownership");
}
public void copy(String v) {
StringSelection fieldContent = new StringSelection(v);
clipboard.setContents(fieldContent, this);
}
public String paste() {
Transferable clipboardContent;
clipboardContent = clipboard.getContents(this);
if (clipboardContent == null
|| !clipboardContent
.isDataFlavorSupported(DataFlavor.stringFlavor))
return "";
String tempString = "";
try {
tempString = (String) clipboardContent
.getTransferData(DataFlavor.stringFlavor);
} catch (UnsupportedFlavorException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
return tempString;
}
public void keyEvent(KeyEvent e) {
//TODO: reinstate this (Tab functionality)?
if (visible)
/*if (e.getID() == java.awt.event.KeyEvent.KEY_PRESSED && e.getKeyCode() == java.awt.Event.TAB) {
if (focusIndex != -1 && getComponentWithFocus() != null)
getComponentWithFocus().actionPerformed(
new Event(getComponentWithFocus(), "Lost Focus"));
if ((e.getModifiersEx() & 0x40) == 64)
giveFocusToPreviousComponent();
else
giveFocusToNextComponent();
if (focusIndex != -1 && getComponentWithFocus() != null)
getComponentWithFocus()
.actionPerformed(
new Event(getComponentWithFocus(),
"Received Focus"));
} else */if (e.getKeyCode() != java.awt.Event.TAB && focusIndex >= 0
&& focusIndex < size())
getComponentWithFocus().keyEvent(e);
}
private void giveFocusToPreviousComponent() {
int oldFocus = focusIndex;
for (focusIndex = (focusIndex - 1) % size(); !getComponentWithFocus()
.canReceiveFocus()
&& focusIndex != oldFocus; focusIndex = (focusIndex - 1)
% size())
;
}
private void giveFocusToNextComponent() {
int oldFocus = focusIndex;
System.out.println("[interfascia.Controller] giveFocusToNextComponent focusIndex:"+focusIndex + " contents.size():"+contents.size());
for (focusIndex = (focusIndex + 1) % size(); !getComponentWithFocus()
.canReceiveFocus()
&& focusIndex != oldFocus; focusIndex = (focusIndex + 1)
% size())
;
}
public int size() {
return contents.size();
}
public void draw() {
draw(0,0);
}
public void draw(int x, int y) {
//Log.debug(this.toString());
papplet.pushMatrix();
papplet.translate(x, y);
if (visible) {
//userState.saveSettingsForApplet(papplet);
//lookAndFeel.defaultGraphicsState.restoreSettingsToApplet(papplet);
for (int i = 0; i < size(); i++)
if (contents.get(i) != null)
contents.get(i).draw();
//userState.restoreSettingsToApplet(papplet);
}
papplet.popMatrix();
}
public String toString() {
return "(GUIController \"" + name + "\") " + contents;
}
}