发布于 2008-04-07 15:03:40
0楼
import java.awt.*;
import java.awt.event.*;
import java.io.PrintStream;
public class hmiElement
implements cellRefHolder
{
public hmiElement()
{
eventNeeds = 0;
colorCodes = new int[9];
flags = 0;
}
public AddressModifier getAddressModifier()
{
return owner.getAddressModifier();
}
public boolean canHandleEvent(MouseEvent evt)
{
return isEventInArea(evt);
}
public boolean isEventInArea(MouseEvent evt)
{
int ex = evt.getX() - x;
int ey = evt.getY() - y;
return ex > 0 && ex < sx && ey > 0 && ey < sy;
}
public static hmiElement newElement(String s, hmiList owner, int number)
{
int tcount = 0;
hmiElement he = null;
for(StringTokenizer st = new StringTokenizer(s, ","); st.hasMoreTokens();)
{
String t = st.nextToken();
if(tcount == 0)
{
if(t.indexOf(".") < 0)
t = "org.visual.hmi.widgets." + t;
try
{
he = (hmiElement)Class.forName(t).newInstance();
}
catch(Exception e)
{
System.out.println(e);
he = new hmiElement();
}
he.owner = owner;
he.colorCodes = new int[9];
he.elNumber = number;
he.Cells = new cellRef[he.needCells()];
} else
{
he.setMore(t, tcount);
}
tcount++;
}
if(tcount <= he.needCells())
System.out.println(he.getClass().getName() + " " + number + ": " + ((he.needCells() - tcount) + 1) + " parameters missing ");
return he;
}
public int needCells()
{
return 16;
}
public void setMore(String s, int n)
{
if(n <= 16)
{
int t;
if(n <= 4)
t = 6;
else
if(n <= 13)
t = 7;
else
t = 9;
Cells[n - 1] = new cellRef(s, this, t);
}
if(n > needCells())
System.out.println("Hint: " + getClass().getName() + " " + elNumber + ": superfluos parameter " + n + ":" + s);
}
public String hintString(MouseEvent e)
{
Class cs = getClass();
String hint = owner.getParameter("Hint" + elNumber);
if(hint == null)
{
String className = cs.getName();
if(className.startsWith("org.visual.hmi.widgets"))
className = className.substring(className.lastIndexOf(".") + 1);
hint = owner.getParameter(className + "Hint");
}
return hint;
}
public void setComError(int flag)
{
comError = flag;
}
public hmiList getList()
{
return owner;
}
public void knob(Graphics g, int body, int border)
{
setGroupedColor(body, g);
g.fillOval(x + sx / 10, y + sy / 10, (sx * 4) / 5, (sy * 4) / 5);
setGroupedColor(border, g);
g.drawOval(x + sx / 10, y + sy / 10, (sx * 4) / 5, (sy * 4) / 5);
}
public void fitText(Graphics g, String S)
{
fitText(g, S, x, y, sx, sy);
}
public void fitText(Graphics g, String S, int x, int y, int xs, int ys)
{
if(S == null)
return;
if(S.length() == 0)
return;
int ffs = 99;
FontMetrics FM = owner.getFontMetrics(ffs);
int i = FM.stringWidth(S);
int xfs = (ffs * (xs - 2)) / i;
i = FM.getHeight();
int yfs = (ffs * (ys - 0)) / i;
if(xfs > yfs)
xfs = yfs;
if(xfs > 99)
xfs = 99;
if(xfs <= 0)
xfs = 1;
FM = owner.getFontMetrics(xfs);
g.setFont(owner.getFont(xfs));
g.drawString(S, x + 2, y + FM.getAscent() + (ys - FM.getHeight()) / 2);
}
public void setParameterColor(int c, Graphics g)
{
int cc = 1;
cc = colorCodes[c - 1];
setCodedColor(cc, g);
}
public Color getParameterColor(int c)
{
int cc = 1;
cc = colorCodes[c - 1];
return getCodedColor(cc);
}
public void setCodedColor(int c, Graphics g)
{
nopaint = false;
if(c == -1)
nopaint = true;
else
g.setColor(getCodedColor(c));
}
public Color getCodedColor(int c)
{
switch(c)
{
case -1:
return null;
case 0: // '\0'
return Color.black;
case 1: // '\001'
return hmiList.myblue;
case 2: // '\002'
return hmiList.mygreen;
case 4: // '\004'
return hmiList.myred;
case 3: // '\003'
if(owner.getBlink())
return hmiList.myblink1;
else
return hmiList.myblink2;
case 5: // '\005'
return hmiList.mymagenta;
case 6: // '\006'
return Color.orange;
case 7: // '\007'
return Color.lightGray;
case 8: // '\b'
return Color.darkGray;
case 9: // '\t'
return Color.blue;
case 10: // '\n'
return Color.green;
case 11: // '\013'
return Color.cyan;
case 12: // '\f'
return Color.red;
case 13: // '\r'
return Color.magenta;
case 14: // '\016'
return Color.yellow;
case 15: // '\017'
return Color.white;
}
return new Color(c);
}
public void setGroupedColor(int c, Graphics g)
{
if(mainValue >= minimum)
c += 3;
if(mainValue > maximum)
c += 3;
setParameterColor(c, g);
}
public void getVal()
{
comError = 0;
x = Cells[0].getInt();
y = Cells[1].getInt();
sx = Cells[2].getInt();
sy = Cells[3].getInt();
for(int i = 0; i < 9; i++)
colorCodes[i] = Cells[i + 4].getInt();
mainValue = Cells[13].getNum();
minimum = Cells[14].getNum();
maximum = Cells[15].getNum();
}
public void paint(Graphics g)
{
background(g);
setGroupedColor(1, g);
paintmore(g);
}
public void paintmore(Graphics g1)
{
}
public void background(Graphics g)
{
setGroupedColor(3, g);
if(!nopaint)
g.fillRect(x, y, sx, sy);
setGroupedColor(2, g);
if(!nopaint)
g.drawRect(x, y, sx, sy);
}
public void ErrorPaint(Graphics g)
{
g.setColor(Color.red);
g.fillRect(x, y, sx, sy);
g.setColor(Color.white);
g.drawRect(x, y, sx, sy);
switch(comError)
{
case 1: // '\001'
fitText(g, "timeout", x, y, sx, sy);
break;
case 2: // '\002'
fitText(g, "not a number", x, y, sx, sy);
break;
case 3: // '\003'
fitText(g, "host!", x, y, sx, sy);
break;
case 4: // '\004'
fitText(g, Cells[13].coord + " unknown!", x, y, sx, sy);
break;
case 5: // '\005'
fitText(g, "???", x, y, sx, sy);
break;
}
}
public void action(MouseEvent e)
{
if(e.isMetaDown())
{
flags ^= 1;
} else
{
flags = 1;
owner.callTrend();
}
}
public boolean usesKey(KeyEvent e)
{
return false;
}
static final int debug = 0;
public int elNumber;
public int eventNeeds;
public int x;
public int y;
public int sx;
public int sy;
public int colorCodes[];
public int comError;
public int flags;
public cellRef Cells[];
public double mainValue;
public double minimum;
public double maximum;
public hmiList owner;
public boolean nopaint;