Make first real commit: copy of CaRMetal 4.2.8
This commit is contained in:
parent
002acfc88e
commit
c312811084
1120 changed files with 226843 additions and 1 deletions
146
eric/controls/JCanvasButton.java
Normal file
146
eric/controls/JCanvasButton.java
Normal file
|
|
@ -0,0 +1,146 @@
|
|||
/*
|
||||
|
||||
Copyright 2006 Eric Hakenholz
|
||||
|
||||
This file is part of C.a.R. software.
|
||||
|
||||
C.a.R. is a free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, version 3 of the License.
|
||||
|
||||
C.a.R. is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
package eric.controls;
|
||||
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
|
||||
import javax.swing.JButton;
|
||||
|
||||
import rene.util.xml.XmlWriter;
|
||||
import rene.zirkel.ZirkelCanvas;
|
||||
import rene.zirkel.objects.ExpressionObject;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author erichake
|
||||
*/
|
||||
public class JCanvasButton extends JCanvasPanel {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID=1L;
|
||||
MyJButton JCB;
|
||||
|
||||
public JCanvasButton(final ZirkelCanvas zc, final ExpressionObject o) {
|
||||
super(zc, o);
|
||||
JSL=new MyJButton();
|
||||
JCB=(MyJButton) JSL;
|
||||
JCB.addMouseListener(this);
|
||||
JCB.addMouseMotionListener(this);
|
||||
final JCanvasButton btn=this;
|
||||
JCB.addMouseListener(new MouseAdapter() {
|
||||
|
||||
@Override
|
||||
public void mouseClicked(final MouseEvent e) {
|
||||
if (!isEditMode()&&!isTargetMode()) {
|
||||
ZC.runControlScripts(btn);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mousePressed(final MouseEvent e) {
|
||||
if (isEditMode()||isTargetMode()) {
|
||||
JCB.setEnabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(final MouseEvent e) {
|
||||
if (!hidden()) {
|
||||
JCB.setEnabled(true);
|
||||
if (getVal()==0) {
|
||||
setVal(1);
|
||||
} else {
|
||||
setVal(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
showval=false;
|
||||
showunit=false;
|
||||
showcom=false;
|
||||
setComment("ok");
|
||||
setVal(0);
|
||||
this.add(JCPlabel);
|
||||
this.add(JCB);
|
||||
this.add(JCPresize);
|
||||
zc.add(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setComment(final String s) {
|
||||
lbl_com=s;
|
||||
JCPlabel.setText(goodLabel());
|
||||
if (showcom) {
|
||||
JCB.setText("");
|
||||
} else {
|
||||
JCB.setText(s);
|
||||
}
|
||||
setDims();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setShowComment(final boolean b) {
|
||||
showcom=b;
|
||||
JCPlabel.setText(goodLabel());
|
||||
if (showcom) {
|
||||
JCB.setText("");
|
||||
} else {
|
||||
JCB.setText(lbl_com);
|
||||
}
|
||||
setDims();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getVal() {
|
||||
double s;
|
||||
try {
|
||||
s=O.getValue();
|
||||
} catch (final Exception ex) {
|
||||
s=0;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
class MyJButton extends JButton {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID=1L;
|
||||
|
||||
MyJButton() {
|
||||
super();
|
||||
setFocusable(false);
|
||||
setOpaque(false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void PrintXmlTags(final XmlWriter xml) {
|
||||
xml.startTagStart("CTRLbutton");
|
||||
|
||||
super.PrintXmlTags(xml);
|
||||
xml.finishTagNewLine();
|
||||
|
||||
}
|
||||
}
|
||||
128
eric/controls/JCanvasCheckBox.java
Normal file
128
eric/controls/JCanvasCheckBox.java
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
/*
|
||||
|
||||
Copyright 2006 Eric Hakenholz
|
||||
|
||||
This file is part of C.a.R. software.
|
||||
|
||||
C.a.R. is a free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, version 3 of the License.
|
||||
|
||||
C.a.R. is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
package eric.controls;
|
||||
|
||||
import java.awt.event.ItemEvent;
|
||||
import java.awt.event.ItemListener;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
|
||||
import javax.swing.JCheckBox;
|
||||
|
||||
import rene.gui.Global;
|
||||
import rene.util.xml.XmlWriter;
|
||||
import rene.zirkel.ZirkelCanvas;
|
||||
import rene.zirkel.objects.ExpressionObject;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author erichake
|
||||
*/
|
||||
public class JCanvasCheckBox extends JCanvasPanel implements ItemListener {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
MyJCheckBox JCB;
|
||||
|
||||
public JCanvasCheckBox(final ZirkelCanvas zc, final ExpressionObject o) {
|
||||
super(zc, o);
|
||||
JSL = new MyJCheckBox();
|
||||
JCB = (MyJCheckBox) JSL;
|
||||
// JCanvasPanel.fixsize(JCB, 50, 22);
|
||||
JCB.addMouseListener(this);
|
||||
JCB.addMouseMotionListener(this);
|
||||
JCB.addItemListener(this);
|
||||
JCB.addMouseListener(new MouseAdapter() {
|
||||
|
||||
@Override
|
||||
public void mousePressed(final MouseEvent e) {
|
||||
if (isEditMode()||isTargetMode()) {
|
||||
JCB.setEnabled(false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(final MouseEvent e) {
|
||||
JCB.setEnabled(true);
|
||||
}
|
||||
});
|
||||
this.add(JCB);
|
||||
showval = false;
|
||||
showunit = false;
|
||||
showcom = true;
|
||||
String s = Global.Loc("props.expl");
|
||||
s = s.trim();
|
||||
s = s.replace(":", "");
|
||||
s = s.trim();
|
||||
setComment(s);
|
||||
setVal(0);
|
||||
this.add(JCPlabel);
|
||||
this.add(JCPresize);
|
||||
zc.add(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getVal() {
|
||||
final double s = JCB.isSelected() ? 1 : 0;
|
||||
return s;
|
||||
}
|
||||
|
||||
public void setSelected(boolean b){
|
||||
JCB.setSelected(b);
|
||||
}
|
||||
|
||||
class MyJCheckBox extends JCheckBox {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
MyJCheckBox() {
|
||||
super();
|
||||
setFocusable(false);
|
||||
setOpaque(false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void itemStateChanged(final ItemEvent arg0) {
|
||||
if(!hidden()){
|
||||
try {
|
||||
final int s = JCB.isSelected() ? 1 : 0;
|
||||
setVal(s);
|
||||
} catch (final Exception ex) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void PrintXmlTags(final XmlWriter xml) {
|
||||
xml.startTagStart("CTRLcheckbox");
|
||||
super.PrintXmlTags(xml);
|
||||
xml.finishTagNewLine();
|
||||
|
||||
}
|
||||
}
|
||||
532
eric/controls/JCanvasPanel.java
Normal file
532
eric/controls/JCanvasPanel.java
Normal file
|
|
@ -0,0 +1,532 @@
|
|||
/*
|
||||
|
||||
Copyright 2006 Eric Hakenholz
|
||||
|
||||
This file is part of C.a.R. software.
|
||||
|
||||
C.a.R. is a free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, version 3 of the License.
|
||||
|
||||
C.a.R. is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
package eric.controls;
|
||||
|
||||
import eric.GUI.palette.PaletteManager;
|
||||
import java.awt.BasicStroke;
|
||||
import java.awt.Color;
|
||||
import java.awt.Cursor;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Font;
|
||||
import java.awt.FontMetrics;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Point;
|
||||
import java.awt.Toolkit;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseListener;
|
||||
import java.awt.event.MouseMotionAdapter;
|
||||
import java.awt.event.MouseMotionListener;
|
||||
import java.awt.geom.Rectangle2D;
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.DecimalFormatSymbols;
|
||||
|
||||
import javax.swing.BorderFactory;
|
||||
import javax.swing.BoxLayout;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JLabel;
|
||||
import eric.JEricPanel;
|
||||
import javax.swing.SwingConstants;
|
||||
import javax.swing.event.ChangeEvent;
|
||||
import javax.swing.event.ChangeListener;
|
||||
|
||||
import rene.gui.Global;
|
||||
import rene.util.xml.XmlWriter;
|
||||
import rene.zirkel.ZirkelCanvas;
|
||||
import rene.zirkel.objects.ExpressionObject;
|
||||
import eric.JPointName;
|
||||
import eric.JZirkelCanvas;
|
||||
import eric.bar.JPropertiesBar;
|
||||
import java.awt.Image;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.awt.image.FilteredImageSource;
|
||||
import java.awt.image.ImageFilter;
|
||||
import javax.swing.GrayFilter;
|
||||
import rene.zirkel.tools.JSmacroTool;
|
||||
import rene.zirkel.tools.Scripts_SetMouseDrag;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author erichake
|
||||
*/
|
||||
public class JCanvasPanel extends JEricPanel implements MouseListener,
|
||||
MouseMotionListener, ChangeListener {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID=1L;
|
||||
ImageIcon ctrlresizeicon=new ImageIcon(getClass().getResource(
|
||||
"/eric/GUI/icons/palette/ctrl_resize.png"));
|
||||
static int COMFONTSIZE=14;
|
||||
static int COMSIZE=50;
|
||||
static Color COMCOLOR=new Color(80, 80, 80);
|
||||
JComponent JSL;
|
||||
String lbl_com="", lbl_val="", lbl_unit="°";
|
||||
boolean showcom=true, showval=true, showunit=false;
|
||||
JLabel JCPlabel=new JLabel();
|
||||
JButton JCPresize=new JButton();
|
||||
// int W, H;
|
||||
ZirkelCanvas ZC;
|
||||
public ExpressionObject O;
|
||||
boolean hidden=false;
|
||||
private boolean showborder1=false;
|
||||
private boolean showborder2=false;
|
||||
private boolean showhandle=false;
|
||||
private MouseEvent pressed;
|
||||
private Point location;
|
||||
private final DecimalFormat decFormat;
|
||||
|
||||
public boolean hidden(){
|
||||
return hidden;
|
||||
}
|
||||
|
||||
public void paint(Graphics g) {
|
||||
Graphics2D g2D=null;
|
||||
BufferedImage sprite=null;
|
||||
if ((!isHidden())||(ZC.getShowHidden())) {
|
||||
sprite=new BufferedImage(getSize().width,
|
||||
getSize().height, BufferedImage.TYPE_INT_ARGB);
|
||||
g2D=sprite.createGraphics();
|
||||
// super.paintChildren(g2D);
|
||||
super.paint(g2D);
|
||||
}
|
||||
if ((isHidden())&&(ZC.getShowHidden())) {
|
||||
final ImageFilter filter=new GrayFilter(true, 60);
|
||||
final Image disImage=this.createImage(new FilteredImageSource(
|
||||
sprite.getSource(), filter));
|
||||
final ImageIcon myicn=new ImageIcon(disImage);
|
||||
g2D.drawImage(myicn.getImage(), 0, 0, getSize().width, getSize().height, this);
|
||||
}
|
||||
if (sprite!=null) {
|
||||
ZC.I.getGraphics().drawImage(sprite, getLocation().x,
|
||||
getLocation().y, this);
|
||||
}
|
||||
// when mouseentered :
|
||||
final Dimension d=getSize();
|
||||
if (O.selected()) {
|
||||
g.setColor(JControlsManager.bordercolor3);
|
||||
g.drawRect(0, 0, d.width-8, d.height-1);
|
||||
}
|
||||
else if(showhandle) {
|
||||
final Graphics2D g2d=(Graphics2D) g;
|
||||
|
||||
final Rectangle2D rectangle=new Rectangle2D.Double(0, 0,
|
||||
d.width-8, d.height-1);
|
||||
g2d.setColor(new Color(119, 136, 153));
|
||||
g2d.setStroke(new BasicStroke(1f, BasicStroke.CAP_SQUARE,
|
||||
BasicStroke.JOIN_BEVEL, 1f, new float[]{2f}, 0f));
|
||||
g2d.draw(rectangle);
|
||||
g.drawImage(ctrlresizeicon.getImage(), d.width-12,
|
||||
d.height/2-4, this);
|
||||
} else {
|
||||
if (showborder1) {
|
||||
g.setColor(JControlsManager.bordercolor1);
|
||||
g.drawRect(0, 0, d.width-8, d.height-1);
|
||||
} else if (showborder2) {
|
||||
g.setColor(JControlsManager.bordercolor2);
|
||||
g.drawRect(0, 0, d.width-8, d.height-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paintComponent(final Graphics g) {
|
||||
|
||||
}
|
||||
|
||||
// withoutExpr unused, just to make another constructor :
|
||||
public JCanvasPanel(final ZirkelCanvas zc, final ExpressionObject o) {
|
||||
super();
|
||||
ZC=zc;
|
||||
O=(o==null)?createExpression():o;
|
||||
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
|
||||
this.setOpaque(false);
|
||||
this.addMouseListener(this);
|
||||
this.addMouseMotionListener(this);
|
||||
|
||||
JCPlabel.setHorizontalAlignment(SwingConstants.LEFT);
|
||||
JCPlabel.setFont(new Font(Global.GlobalFont, 0, COMFONTSIZE));
|
||||
JCPlabel.setForeground(COMCOLOR);
|
||||
|
||||
// JCPresize.setIcon(ctrlresizeicon);
|
||||
JCPresize.setOpaque(false);
|
||||
JCPresize.setContentAreaFilled(false);
|
||||
JCPresize.setBorder(BorderFactory.createEmptyBorder());
|
||||
JCPresize.setCursor(new Cursor(Cursor.E_RESIZE_CURSOR));
|
||||
|
||||
JCPresize.addMouseListener(new MouseAdapter() {
|
||||
|
||||
@Override
|
||||
public void mousePressed(final MouseEvent me) {
|
||||
pressed=me;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(final MouseEvent me) {
|
||||
ZC.JCM.hideBorders((JCanvasPanel) ((JComponent) me.getSource()).getParent());
|
||||
}
|
||||
});
|
||||
|
||||
JCPresize.addMouseMotionListener(new MouseMotionAdapter() {
|
||||
|
||||
@Override
|
||||
public void mouseDragged(final MouseEvent me) {
|
||||
final int w=JSL.getSize().width+me.getX();
|
||||
setDims(w-pressed.getX(), getSize().height);
|
||||
ZC.JCM.analyseResize((JCanvasPanel) ((JComponent) me.getSource()).getParent());
|
||||
}
|
||||
});
|
||||
|
||||
decFormat=new DecimalFormat();
|
||||
final DecimalFormatSymbols dfs=new DecimalFormatSymbols();
|
||||
dfs.setDecimalSeparator('.');
|
||||
decFormat.setDecimalFormatSymbols(dfs);
|
||||
}
|
||||
|
||||
private ExpressionObject createExpression() {
|
||||
final ExpressionObject o=new ExpressionObject(ZC.getConstruction(),
|
||||
0, 0);
|
||||
o.setDefaults();
|
||||
o.setSuperHidden(true);
|
||||
o.setOwnedByControl(true);
|
||||
ZC.getConstruction().add(o);
|
||||
|
||||
if (JZirkelCanvas.getCurrentJZF()!=null) {
|
||||
final int i=JZirkelCanvas.getCurrentJZF().getPointLabel().getCurrentLetterSetCode();
|
||||
final boolean b=Global.getParameter("options.point.shownames",
|
||||
false);
|
||||
Global.setParameter("options.point.shownames", true);
|
||||
final String s=JZirkelCanvas.getCurrentJZF().getPointLabel().setLetterSet(JPointName.minLettersSetCode);
|
||||
|
||||
Global.setParameter("options.point.shownames", b);
|
||||
JZirkelCanvas.getCurrentJZF().getPointLabel().setLetterSet(i);
|
||||
o.setName(s);
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
public int StringwWidth(final String s) {
|
||||
// FontMetrics fm = getFontMetrics(getFont());
|
||||
final FontMetrics fm=getFontMetrics(new Font(Global.GlobalFont, 0,
|
||||
COMFONTSIZE));
|
||||
|
||||
return fm.stringWidth(s);
|
||||
}
|
||||
|
||||
// MUST BE OVERRIDE :
|
||||
public double getVal() {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
public void setOnlyValue(final double x) {
|
||||
decFormat.setMaximumFractionDigits(Global.getParameter("digits.edit", 5));
|
||||
lbl_val=String.valueOf(decFormat.format(x));
|
||||
}
|
||||
|
||||
public void setVal(final double x) {
|
||||
setOnlyValue(x);
|
||||
setVal(String.valueOf(x));
|
||||
}
|
||||
|
||||
public void setVal(final String s) {
|
||||
JCPlabel.setText(goodLabel());
|
||||
try {
|
||||
O.setExpression(s, ZC.getConstruction());
|
||||
ZC.recompute();
|
||||
setDims();
|
||||
// ZC.validate();
|
||||
// ZC.repaint();
|
||||
} catch (final Exception ex) {
|
||||
}
|
||||
}
|
||||
|
||||
public void setDims(final int x, final int y, final int w, final int h) {
|
||||
final int W=w+StringwWidth(JCPlabel.getText())+15;
|
||||
fixsize(JCPlabel, StringwWidth(JCPlabel.getText()), h);
|
||||
fixsize(JCPresize, 15, h);
|
||||
fixsize(JSL, w, h);
|
||||
revalidate();
|
||||
setBounds(x, y, W, h);
|
||||
ZC.validate();
|
||||
ZC.repaint();
|
||||
}
|
||||
|
||||
public void setDims(final int w, final int h) {
|
||||
final int x=getLocation().x;
|
||||
final int y=getLocation().y;
|
||||
setDims(x, y, w, h);
|
||||
}
|
||||
|
||||
public void setDims() {
|
||||
setDims(JSL.getSize().width, JSL.getSize().height);
|
||||
}
|
||||
|
||||
public void grow(final int w, final int h) {
|
||||
setDims(JSL.getSize().width+w, JSL.getSize().height+h);
|
||||
}
|
||||
|
||||
public String getComment() {
|
||||
return lbl_com;
|
||||
}
|
||||
|
||||
public void setComment(final String s) {
|
||||
lbl_com=s;
|
||||
JCPlabel.setText(goodLabel());
|
||||
setDims();
|
||||
}
|
||||
|
||||
public String getUnit() {
|
||||
return lbl_unit;
|
||||
}
|
||||
|
||||
public void setUnit(final String s) {
|
||||
lbl_unit=s;
|
||||
JCPlabel.setText(goodLabel());
|
||||
setDims();
|
||||
}
|
||||
|
||||
public void setShowComment(final boolean b) {
|
||||
showcom=b;
|
||||
JCPlabel.setText(goodLabel());
|
||||
setDims();
|
||||
}
|
||||
|
||||
public void setShowVal(final boolean b) {
|
||||
showval=b;
|
||||
JCPlabel.setText(goodLabel());
|
||||
setDims();
|
||||
}
|
||||
|
||||
public void setShowUnit(final boolean b) {
|
||||
showunit=b;
|
||||
JCPlabel.setText(goodLabel());
|
||||
setDims();
|
||||
}
|
||||
|
||||
public boolean getShowComment() {
|
||||
return showcom;
|
||||
}
|
||||
|
||||
public boolean getShowVal() {
|
||||
return showval;
|
||||
}
|
||||
|
||||
public boolean getShowUnit() {
|
||||
return showunit;
|
||||
}
|
||||
|
||||
public String goodLabel() {
|
||||
String lbl="";
|
||||
if (showcom) {
|
||||
lbl+=lbl_com;
|
||||
}
|
||||
if (showval) {
|
||||
lbl+=lbl_val;
|
||||
}
|
||||
if (showunit) {
|
||||
lbl+=lbl_unit;
|
||||
}
|
||||
return lbl;
|
||||
}
|
||||
|
||||
static public void fixsize(final JComponent cp, final int w, final int h) {
|
||||
final Dimension d=new Dimension(w, h);
|
||||
cp.setMaximumSize(d);
|
||||
cp.setMinimumSize(d);
|
||||
cp.setPreferredSize(d);
|
||||
cp.setSize(d);
|
||||
}
|
||||
|
||||
public boolean isTargetMode(){
|
||||
return ((ZC.getTool() instanceof Scripts_SetMouseDrag)&&(this instanceof JCanvasButton));
|
||||
}
|
||||
|
||||
public boolean isEditMode() {
|
||||
if (JZirkelCanvas.getCurrentJZF()==null) {
|
||||
return false;
|
||||
}
|
||||
boolean bool=(PaletteManager.isSelected("edit"))
|
||||
||(PaletteManager.isSelected("ctrl_edit"));
|
||||
bool=(bool||(PaletteManager.isSelected("ctrl_slider")));
|
||||
bool=(bool||(PaletteManager.isSelected("ctrl_popup")));
|
||||
bool=(bool||(PaletteManager.isSelected("ctrl_chkbox")));
|
||||
bool=(bool||(PaletteManager.isSelected("ctrl_txtfield")));
|
||||
bool=(bool||(PaletteManager.isSelected("ctrl_button")));
|
||||
bool=(bool||(PaletteManager.isSelected("delete")));
|
||||
bool=(bool||(PaletteManager.isSelected("hide")));
|
||||
return bool;
|
||||
}
|
||||
|
||||
public boolean isDeleteMode() {
|
||||
if (JZirkelCanvas.getCurrentJZF()==null) {
|
||||
return false;
|
||||
}
|
||||
return (PaletteManager.isSelected("delete"));
|
||||
}
|
||||
|
||||
public boolean isHideToolSelected() {
|
||||
if (JZirkelCanvas.getCurrentJZF()==null) {
|
||||
return false;
|
||||
}
|
||||
return (PaletteManager.isSelected("hide"));
|
||||
}
|
||||
|
||||
public boolean isHidden() {
|
||||
return (hidden||O.testConditional("hidden"));
|
||||
}
|
||||
|
||||
public void setHidden(final boolean b) {
|
||||
hidden=b;
|
||||
}
|
||||
|
||||
public void showBorder() {
|
||||
showborder2=true;
|
||||
repaint();
|
||||
}
|
||||
|
||||
public void hideBorder() {
|
||||
showborder2=false;
|
||||
repaint();
|
||||
}
|
||||
|
||||
public void showHandle() {
|
||||
showhandle=true;
|
||||
|
||||
setDims();
|
||||
}
|
||||
|
||||
public void hideHandle() {
|
||||
showhandle=false;
|
||||
setDims();
|
||||
}
|
||||
|
||||
public void mouseClicked(final MouseEvent arg0) {
|
||||
}
|
||||
|
||||
public void mousePressed(final MouseEvent e) {
|
||||
pressed=e;
|
||||
if (e.isPopupTrigger()) {
|
||||
return;
|
||||
}
|
||||
ZC.JCM.hideHandles(null);
|
||||
}
|
||||
|
||||
public void mouseReleased(final MouseEvent e) {
|
||||
if (e.isPopupTrigger()) {
|
||||
return;
|
||||
}
|
||||
if ((!ZC.getShowHidden())&&(isHidden())) {
|
||||
|
||||
return;
|
||||
}
|
||||
if (isHideToolSelected()) {
|
||||
setHidden(!isHidden());
|
||||
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
|
||||
showborder1=false;
|
||||
repaint();
|
||||
}
|
||||
if (isDeleteMode()) {
|
||||
ZC.JCM.removeControl(this);
|
||||
}
|
||||
|
||||
ZC.JCM.hideBorders(this);
|
||||
if ((isEditMode())&&(!isHidden())) {
|
||||
showHandle();
|
||||
JPropertiesBar.EditObject(this);
|
||||
}
|
||||
if (isTargetMode()) {
|
||||
Scripts_SetMouseDrag tool=(Scripts_SetMouseDrag) ZC.getTool();
|
||||
tool.addFromControl(O, ZC);
|
||||
repaint();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void mouseEntered(final MouseEvent arg0) {
|
||||
if (isHidden()) {
|
||||
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
|
||||
showborder1=false;
|
||||
// repaint();
|
||||
return;
|
||||
}
|
||||
if (isTargetMode()||isEditMode()) {
|
||||
setCursor(new Cursor(Cursor.HAND_CURSOR));
|
||||
showborder1=true;
|
||||
repaint();
|
||||
}
|
||||
}
|
||||
|
||||
public void mouseExited(final MouseEvent arg0) {
|
||||
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
|
||||
showborder1=false;
|
||||
repaint();
|
||||
}
|
||||
|
||||
public void mouseDragged(final MouseEvent me) {
|
||||
if (!isEditMode()) {
|
||||
return;
|
||||
}
|
||||
|
||||
location=getLocation(location);
|
||||
int x=location.x-pressed.getX()+me.getX();
|
||||
int y=location.y-pressed.getY()+me.getY();
|
||||
if (x<0) {
|
||||
x=0;
|
||||
} else if (x+getSize().width>ZC.getSize().width) {
|
||||
x=ZC.getSize().width-getSize().width;
|
||||
}
|
||||
if (y<0) {
|
||||
y=0;
|
||||
} else if (y+getSize().height>ZC.getSize().height) {
|
||||
y=ZC.getSize().height-getSize().height;
|
||||
}
|
||||
setLocation(x, y);
|
||||
Toolkit.getDefaultToolkit().sync();
|
||||
ZC.JCM.analyseXY(this);
|
||||
}
|
||||
|
||||
public void mouseMoved(final MouseEvent arg0) {
|
||||
}
|
||||
|
||||
// Change event from JSlider :
|
||||
public void stateChanged(final ChangeEvent arg0) {
|
||||
}
|
||||
|
||||
public void PrintXmlTags(final XmlWriter xml) {
|
||||
|
||||
xml.printArg("Ename", ""+O.getName());
|
||||
xml.printArg("x", ""+getLocation().x);
|
||||
xml.printArg("y", ""+getLocation().y);
|
||||
xml.printArg("w", ""+JSL.getSize().width);
|
||||
xml.printArg("h", ""+JSL.getSize().height);
|
||||
xml.printArg("showC", ""+showcom);
|
||||
xml.printArg("showU", ""+showunit);
|
||||
xml.printArg("showV", ""+showval);
|
||||
xml.printArg("hidden", ""+hidden);
|
||||
xml.printArg("C", ""+lbl_com);
|
||||
xml.printArg("U", ""+lbl_unit);
|
||||
xml.printArg("V", ""+lbl_val);
|
||||
|
||||
}
|
||||
}
|
||||
171
eric/controls/JCanvasPopup.java
Normal file
171
eric/controls/JCanvasPopup.java
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
/*
|
||||
|
||||
Copyright 2006 Eric Hakenholz
|
||||
|
||||
This file is part of C.a.R. software.
|
||||
|
||||
C.a.R. is a free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, version 3 of the License.
|
||||
|
||||
C.a.R. is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
package eric.controls;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
|
||||
import javax.swing.DefaultComboBoxModel;
|
||||
import javax.swing.JComboBox;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.plaf.ComboBoxUI;
|
||||
import javax.swing.plaf.ComponentUI;
|
||||
import javax.swing.plaf.metal.MetalComboBoxUI;
|
||||
|
||||
import rene.gui.Global;
|
||||
import rene.util.xml.XmlWriter;
|
||||
import rene.zirkel.ZirkelCanvas;
|
||||
import rene.zirkel.objects.ExpressionObject;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author erichake
|
||||
*/
|
||||
public class JCanvasPopup extends JCanvasPanel implements ActionListener {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
MyJComboBox JCB;
|
||||
|
||||
public JCanvasPopup(final ZirkelCanvas zc, final ExpressionObject o) {
|
||||
super(zc, o);
|
||||
JSL = new MyJComboBox();
|
||||
JCB = (MyJComboBox) JSL;
|
||||
JCB.setUI((ComboBoxUI) MyComboBoxUI.createUI(JCB));
|
||||
// JCanvasPanel.fixsize(JCB, 100, 18);
|
||||
// addMouseEvents();
|
||||
JCB.addMouseListener(this);
|
||||
JCB.addMouseMotionListener(this);
|
||||
JCB.addMouseListener(new MouseAdapter() {
|
||||
|
||||
@Override
|
||||
public void mousePressed(final MouseEvent e) {
|
||||
if (isEditMode()) {
|
||||
JCB.hidePopup();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
});
|
||||
addMouseEvents();
|
||||
JCB.addActionListener(this);
|
||||
showval = false;
|
||||
showunit = false;
|
||||
showcom = true;
|
||||
setComment(Global.Loc("props.expl") + " ");
|
||||
setVal(1);
|
||||
this.add(JCPlabel);
|
||||
this.add(JCB);
|
||||
this.add(JCPresize);
|
||||
zc.add(this);
|
||||
}
|
||||
|
||||
static class MyComboBoxUI extends MetalComboBoxUI {
|
||||
|
||||
public static ComponentUI createUI(final JComponent c) {
|
||||
return new MyComboBoxUI();
|
||||
}
|
||||
}
|
||||
|
||||
public void addMouseEvents() {
|
||||
for (int i = 0; i < JCB.getComponentCount(); i++) {
|
||||
JCB.getComponent(i).addMouseListener(new MouseAdapter() {
|
||||
|
||||
@Override
|
||||
public void mousePressed(final MouseEvent e) {
|
||||
if (isEditMode()) {
|
||||
JCB.hidePopup();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
});
|
||||
JCB.getComponent(i).addMouseListener(this);
|
||||
JCB.getComponent(i).addMouseMotionListener(this);
|
||||
}
|
||||
}
|
||||
|
||||
public String getItems() {
|
||||
String s = "";
|
||||
for (int i = 0; i < JCB.getItemCount() - 1; i++) {
|
||||
s += JCB.getItemAt(i) + "\n";
|
||||
}
|
||||
s += JCB.getItemAt(JCB.getItemCount() - 1);
|
||||
return s;
|
||||
}
|
||||
|
||||
public void setItems(final String s) {
|
||||
JCB.removeAllItems();
|
||||
final String[] itms = s.split("\n");
|
||||
for (final String itm : itms) {
|
||||
JCB.addItem(itm);
|
||||
}
|
||||
setDims();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getVal() {
|
||||
return (JCB.getSelectedIndex() + 1);
|
||||
}
|
||||
|
||||
class MyJComboBox extends JComboBox {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
MyJComboBox() {
|
||||
super();
|
||||
setFocusable(false);
|
||||
|
||||
setModel(new DefaultComboBoxModel(new String[] { "Item 1",
|
||||
"Item 2", "Item 3" }));
|
||||
}
|
||||
}
|
||||
|
||||
public void actionPerformed(final ActionEvent e) {
|
||||
if (e.getSource() == JCB) {
|
||||
final int selectedIndex = JCB.getSelectedIndex() + 1;
|
||||
|
||||
try {
|
||||
setVal(selectedIndex);
|
||||
} catch (final Exception ex) {
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void PrintXmlTags(final XmlWriter xml) {
|
||||
xml.startTagStart("CTRLpopup");
|
||||
super.PrintXmlTags(xml);
|
||||
final String s = getItems().replace("\n", "@@@");
|
||||
xml.printArg("Items", "" + s);
|
||||
xml.finishTagNewLine();
|
||||
|
||||
}
|
||||
}
|
||||
249
eric/controls/JCanvasSlider.java
Normal file
249
eric/controls/JCanvasSlider.java
Normal file
|
|
@ -0,0 +1,249 @@
|
|||
/*
|
||||
|
||||
Copyright 2006 Eric Hakenholz
|
||||
|
||||
This file is part of C.a.R. software.
|
||||
|
||||
C.a.R. is a free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, version 3 of the License.
|
||||
|
||||
C.a.R. is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
package eric.controls;
|
||||
|
||||
import javax.swing.JSlider;
|
||||
import javax.swing.event.ChangeEvent;
|
||||
import javax.swing.event.ChangeListener;
|
||||
|
||||
import rene.util.xml.XmlWriter;
|
||||
import rene.zirkel.ZirkelCanvas;
|
||||
import rene.zirkel.objects.ExpressionObject;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author erichake
|
||||
*/
|
||||
public class JCanvasSlider extends JCanvasPanel {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
static int PREFEREDVMAX = 10000;
|
||||
int VMAX = PREFEREDVMAX;
|
||||
int TICKS = 1000;
|
||||
double xMIN, xMAX, xTICKS;
|
||||
MyJSlider JCS;
|
||||
|
||||
public JCanvasSlider(final ZirkelCanvas zc, final ExpressionObject o,
|
||||
final double min, final double max, final double val) {
|
||||
super(zc, o);
|
||||
xMIN = min;
|
||||
xMAX = max;
|
||||
xTICKS = getCurrentTicks();
|
||||
setVal(val);
|
||||
JSL = new MyJSlider(0, VMAX, TICKS, (int) Math.round((val - xMIN)
|
||||
* VMAX / (xMAX - xMIN)));
|
||||
JSL.putClientProperty("JComponent.sizeVariant", "regular");
|
||||
JCS = (MyJSlider) JSL;
|
||||
JCS.addMouseListener(this);
|
||||
JCS.addMouseMotionListener(this);
|
||||
setComment(O.getName() + "=");
|
||||
this.add(JCS);
|
||||
this.add(JCPlabel);
|
||||
this.add(JCPresize);
|
||||
zc.add(this);
|
||||
}
|
||||
|
||||
public void setGoodKnobPos(final double x) {
|
||||
final int i = (int) Math.round((x - xMIN) * (VMAX / (xMAX - xMIN)));
|
||||
JCS.setValue(i);
|
||||
}
|
||||
|
||||
public double getCurrentTicks() {
|
||||
return TICKS * (xMAX - xMIN) / (VMAX);
|
||||
}
|
||||
|
||||
public void setTicks(final String s) {
|
||||
setTicks(Double.parseDouble(s));
|
||||
}
|
||||
|
||||
public void setTicks(final double x) {
|
||||
xTICKS = x;
|
||||
adjustVirtualMax();
|
||||
TICKS = (int) Math.round(x * VMAX / (xMAX - xMIN));
|
||||
JCS.setMinorTickSpacing(TICKS);
|
||||
this.revalidate();
|
||||
this.repaint();
|
||||
}
|
||||
|
||||
public String getTicks() {
|
||||
return String.valueOf(xTICKS);
|
||||
}
|
||||
|
||||
public void adjustVirtualMax() {
|
||||
if ((xMAX - xMIN) < 1) {
|
||||
return;
|
||||
}
|
||||
final int mySQRT = (int) Math.round(Math.sqrt(PREFEREDVMAX));
|
||||
|
||||
// VMAX/(xMAX-xMIN) must be an integer :
|
||||
VMAX = (int) Math.round(Math.ceil(mySQRT / (xMAX - xMIN))
|
||||
* (xMAX - xMIN));
|
||||
// VMAX/xTICKS must also be an integer :
|
||||
VMAX *= (int) Math.round(Math.ceil(mySQRT / xTICKS) * xTICKS);
|
||||
JCS.setMaximum(VMAX);
|
||||
}
|
||||
|
||||
public void setMax(final String s) {
|
||||
setMax(Double.parseDouble(s));
|
||||
}
|
||||
|
||||
public void setMax(final double x) {
|
||||
xMAX = x;
|
||||
if (xMIN > xMAX) {
|
||||
xMIN = xMAX - 10;
|
||||
}
|
||||
adjustVirtualMax();
|
||||
double newval = (getVal() > xMAX) ? xMAX : getVal();
|
||||
newval = (getVal() < xMIN) ? xMIN : getVal();
|
||||
setVal(newval);
|
||||
JCS.setValue((int) Math.round((newval - xMIN) * VMAX / (xMAX - xMIN)));
|
||||
setTicks(xTICKS);
|
||||
this.revalidate();
|
||||
this.repaint();
|
||||
}
|
||||
|
||||
public String getMax() {
|
||||
return String.valueOf(xMAX);
|
||||
}
|
||||
|
||||
public void setMin(final String s) {
|
||||
setMin(Double.parseDouble(s));
|
||||
}
|
||||
|
||||
public void setMin(final double x) {
|
||||
xMIN = x;
|
||||
if (xMIN > xMAX) {
|
||||
xMAX = xMIN + 10;
|
||||
}
|
||||
adjustVirtualMax();
|
||||
double newval = (getVal() > xMAX) ? xMAX : getVal();
|
||||
newval = (getVal() < xMIN) ? xMIN : getVal();
|
||||
setVal(newval);
|
||||
JCS.setValue((int) Math.round((newval - xMIN) * VMAX / (xMAX - xMIN)));
|
||||
setTicks(xTICKS);
|
||||
this.revalidate();
|
||||
this.repaint();
|
||||
}
|
||||
|
||||
public String getMin() {
|
||||
return String.valueOf(xMIN);
|
||||
}
|
||||
|
||||
public void setSnap(final boolean b) {
|
||||
JCS.setSnapToTicks(b);
|
||||
this.revalidate();
|
||||
this.repaint();
|
||||
}
|
||||
|
||||
public boolean getSnap() {
|
||||
return JCS.getSnapToTicks();
|
||||
}
|
||||
|
||||
public void setShowTicks(final boolean b) {
|
||||
JCS.setPaintTicks(b);
|
||||
this.revalidate();
|
||||
this.repaint();
|
||||
}
|
||||
|
||||
public boolean getShowTicks() {
|
||||
return JCS.getPaintTicks();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getVal() {
|
||||
return xMIN + JCS.getValue() * (xMAX - xMIN) / VMAX;
|
||||
}
|
||||
|
||||
class MyJSlider extends JSlider implements ChangeListener {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
int oldvalue;
|
||||
|
||||
MyJSlider(final int min, final int max, final int ticks, final int val) {
|
||||
super(min, max, val);
|
||||
oldvalue = val;
|
||||
this.setOpaque(false);
|
||||
this.setFocusable(false);
|
||||
|
||||
// this.setMajorTickSpacing((max-min)/5);
|
||||
this.setMinorTickSpacing(ticks);
|
||||
this.setPaintTicks(true);
|
||||
this.setSnapToTicks(true);
|
||||
this.setOpaque(false);
|
||||
|
||||
this.addChangeListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getValue() {
|
||||
|
||||
if (isEditMode()) {
|
||||
return oldvalue;
|
||||
}
|
||||
oldvalue = super.getValue();
|
||||
return oldvalue;
|
||||
|
||||
}
|
||||
|
||||
public void stateChanged(final ChangeEvent e) {
|
||||
try {
|
||||
if (!hidden()){
|
||||
double val = getValue();
|
||||
val = xMIN + ((xMAX - xMIN) / VMAX) * val;
|
||||
if (getSnapToTicks()) {
|
||||
// snap to good value :
|
||||
final int i = (int) Math.round((val - xMIN) / xTICKS);
|
||||
val = xMIN + xTICKS * i;
|
||||
// eliminate side effects :
|
||||
final double ex = Math.pow(10, 5 - Math.round(Math
|
||||
.log10(val)));
|
||||
val = Math.round(val * ex) / ex;
|
||||
if (Double.isNaN(val)) {
|
||||
val = 0;
|
||||
}
|
||||
}
|
||||
|
||||
setVal(val);
|
||||
}
|
||||
} catch (final Exception ex) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void PrintXmlTags(final XmlWriter xml) {
|
||||
xml.startTagStart("CTRLslider");
|
||||
super.PrintXmlTags(xml);
|
||||
xml.printArg("min", "" + xMIN);
|
||||
xml.printArg("max", "" + xMAX);
|
||||
xml.printArg("T", "" + xTICKS);
|
||||
xml.printArg("fixT", "" + JCS.getSnapToTicks());
|
||||
xml.printArg("showT", "" + JCS.getPaintTicks());
|
||||
xml.finishTagNewLine();
|
||||
}
|
||||
}
|
||||
143
eric/controls/JCanvasTxtfield.java
Normal file
143
eric/controls/JCanvasTxtfield.java
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
/*
|
||||
|
||||
Copyright 2006 Eric Hakenholz
|
||||
|
||||
This file is part of C.a.R. software.
|
||||
|
||||
C.a.R. is a free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, version 3 of the License.
|
||||
|
||||
C.a.R. is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
package eric.controls;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.event.FocusAdapter;
|
||||
import java.awt.event.FocusEvent;
|
||||
import java.awt.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
|
||||
import javax.swing.JTextField;
|
||||
|
||||
import rene.util.xml.XmlWriter;
|
||||
import rene.zirkel.ZirkelCanvas;
|
||||
import rene.zirkel.objects.ExpressionObject;
|
||||
import rene.gui.Global;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author erichake
|
||||
*/
|
||||
public class JCanvasTxtfield extends JCanvasPanel {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
MyJTextField JCB;
|
||||
Color errColor = new Color(201, 68, 27);
|
||||
Color goodColor = new Color(50, 50, 50);
|
||||
|
||||
public JCanvasTxtfield(final ZirkelCanvas zc, final ExpressionObject o) {
|
||||
super(zc, o);
|
||||
JSL = new MyJTextField();
|
||||
JCB = (MyJTextField) JSL;
|
||||
JCB.addKeyListener(new KeyAdapter() {
|
||||
|
||||
@Override
|
||||
public void keyPressed(final KeyEvent e) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(final KeyEvent e) {
|
||||
setVal(Global.Comma_To_Point(JCB.getText(), O
|
||||
.getConstruction(), true));
|
||||
if (O.getExp().isValid()) {
|
||||
JCB.setForeground(goodColor);
|
||||
} else {
|
||||
JCB.setForeground(errColor);
|
||||
}
|
||||
}
|
||||
});
|
||||
JCB.addFocusListener(new FocusAdapter() {
|
||||
|
||||
@Override
|
||||
public void focusGained(final FocusEvent e) {
|
||||
JCB.selectAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void focusLost(final FocusEvent e) {
|
||||
}
|
||||
});
|
||||
JCB.addMouseListener(this);
|
||||
JCB.addMouseMotionListener(this);
|
||||
JCB.addMouseListener(new MouseAdapter() {
|
||||
|
||||
@Override
|
||||
public void mousePressed(final MouseEvent e) {
|
||||
if (isEditMode()) {
|
||||
JCB.setFocusable(false);
|
||||
JCB.setEnabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(final MouseEvent e) {
|
||||
if (!JCB.isEnabled()&&!hidden()) {
|
||||
JCB.setEnabled(true);
|
||||
JCB.setFocusable(true);
|
||||
}
|
||||
}
|
||||
});
|
||||
showval = false;
|
||||
showunit = false;
|
||||
showcom = true;
|
||||
setComment(O.getName() + " = ");
|
||||
setVal(1);
|
||||
JCB.setText("1");
|
||||
JCB.setForeground(goodColor);
|
||||
this.add(JCPlabel);
|
||||
this.add(JCB);
|
||||
this.add(JCPresize);
|
||||
zc.add(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getVal() {
|
||||
return (Double.valueOf(JCB.getText()));
|
||||
}
|
||||
|
||||
class MyJTextField extends JTextField {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
JTextField jtf;
|
||||
|
||||
MyJTextField() {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void PrintXmlTags(final XmlWriter xml) {
|
||||
xml.startTagStart("CTRLtxtfield");
|
||||
super.PrintXmlTags(xml);
|
||||
xml.printArg("txt", "" + JCB.getText());
|
||||
xml.finishTagNewLine();
|
||||
}
|
||||
}
|
||||
450
eric/controls/JControlsManager.java
Normal file
450
eric/controls/JControlsManager.java
Normal file
|
|
@ -0,0 +1,450 @@
|
|||
/*
|
||||
|
||||
Copyright 2006 Eric Hakenholz
|
||||
|
||||
This file is part of C.a.R. software.
|
||||
|
||||
C.a.R. is a free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, version 3 of the License.
|
||||
|
||||
C.a.R. is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
package eric.controls;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.util.ArrayList;
|
||||
import rene.util.xml.XmlTag;
|
||||
import rene.util.xml.XmlWriter;
|
||||
import rene.zirkel.ZirkelCanvas;
|
||||
import rene.zirkel.objects.ExpressionObject;
|
||||
import rene.zirkel.objects.TextObject;
|
||||
import eric.GUI.palette.PaletteManager;
|
||||
import eric.JZirkelCanvas;
|
||||
import eric.bar.JPropertiesBar;
|
||||
import rene.gui.Global;
|
||||
import rene.zirkel.objects.ConstructionObject;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author erichake
|
||||
*/
|
||||
public class JControlsManager {
|
||||
|
||||
static Color bordercolor1=new Color(80, 80, 80);
|
||||
static Color bordercolor2=new Color(180, 180, 250);
|
||||
static Color bordercolor3=new Color(255, 25, 25);
|
||||
static int MAGNET=10;
|
||||
ZirkelCanvas ZC;
|
||||
public ArrayList<JCanvasPanel> CPs=new ArrayList();
|
||||
Rectangle r=new Rectangle();
|
||||
Rectangle r2=new Rectangle();
|
||||
ArrayList<XmlTag> XmlTags=new ArrayList();
|
||||
|
||||
public JControlsManager(final ZirkelCanvas zc) {
|
||||
ZC=zc;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void addControl(final JCanvasPanel jcp, final boolean editme,
|
||||
final int x, final int y, final int w, final int h) {
|
||||
jcp.setDims(x, y, w, h);
|
||||
CPs.add(jcp);
|
||||
ZC.add(jcp);
|
||||
showHandles(jcp);
|
||||
ZC.revalidate();
|
||||
if (editme) {
|
||||
JPropertiesBar.EditObject(jcp);
|
||||
}
|
||||
}
|
||||
|
||||
public JCanvasCheckBox addChkBox(final ExpressionObject o, final int x,
|
||||
final int y, final int w, final int h) {
|
||||
final JCanvasCheckBox jcb=new JCanvasCheckBox(ZC, o);
|
||||
addControl(jcb, o==null, x, y, w, h);
|
||||
return jcb;
|
||||
}
|
||||
|
||||
public JCanvasButton addButton(final ExpressionObject o, final int x,
|
||||
final int y, final int w, final int h) {
|
||||
final JCanvasButton jcb=new JCanvasButton(ZC, o);
|
||||
addControl(jcb, o==null, x, y, w, h);
|
||||
return jcb;
|
||||
}
|
||||
|
||||
public JCanvasTxtfield addTxtField(final ExpressionObject o, final int x,
|
||||
final int y, final int w, final int h) {
|
||||
final JCanvasTxtfield jcb=new JCanvasTxtfield(ZC, o);
|
||||
addControl(jcb, o==null, x, y, w, h);
|
||||
return jcb;
|
||||
}
|
||||
|
||||
public JCanvasSlider addSlider(final ExpressionObject o, final int x,
|
||||
final int y, final int w, final int h) {
|
||||
final JCanvasSlider jcs=new JCanvasSlider(ZC, o, -5, 5, -2);
|
||||
addControl(jcs, o==null, x, y, w, h);
|
||||
return jcs;
|
||||
}
|
||||
|
||||
public JCanvasPopup addPopup(final ExpressionObject o, final int x,
|
||||
final int y, final int w, final int h) {
|
||||
final JCanvasPopup jcp=new JCanvasPopup(ZC, o);
|
||||
addControl(jcp, o==null, x, y, w, h);
|
||||
return jcp;
|
||||
}
|
||||
|
||||
public void analyseResize(final JCanvasPanel jp) {
|
||||
r=jp.getBounds(r);
|
||||
for (int i=0; i<CPs.size(); i++) {
|
||||
final JCanvasPanel jp2=(JCanvasPanel) CPs.get(i);
|
||||
if (!jp2.equals(jp)) {
|
||||
r2=jp2.getBounds(r2);
|
||||
if (Math.abs(r2.x+r2.width-r.x-r.width)<MAGNET) {
|
||||
jp.grow(r2.x+r2.width-r.x-r.width, 0);
|
||||
showBordersRight(jp);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
showBordersRight(jp);
|
||||
}
|
||||
|
||||
public void analyseXY(final JCanvasPanel jp) {
|
||||
r=jp.getBounds(r);
|
||||
for (int i=0; i<CPs.size(); i++) {
|
||||
final JCanvasPanel jp2=(JCanvasPanel) CPs.get(i);
|
||||
if (!jp2.equals(jp)) {
|
||||
r2=jp2.getBounds(r2);
|
||||
// LEFT
|
||||
if (Math.abs(r2.x-r.x)<MAGNET) {
|
||||
jp.setLocation(r2.x, r.y);
|
||||
r=jp.getBounds(r);
|
||||
}
|
||||
// RIGHT
|
||||
if (Math.abs(r2.x+r2.width-r.x-r.width)<MAGNET) {
|
||||
jp.setLocation(r2.x+r2.width-r.width, r.y);
|
||||
r=jp.getBounds(r);
|
||||
}
|
||||
// //HCENTER
|
||||
// if (Math.abs(r2.x+r2.width/2-r.x-r.width/2)<MAGNET) {
|
||||
// jp.setLocation(r2.x+r2.width/2-r.width/2, r.y);
|
||||
// r=jp.getBounds(r);
|
||||
// }
|
||||
// //TOP
|
||||
// if (Math.abs(r2.y-r.y)<MAGNET) {
|
||||
// jp.setLocation(r.x, r2.y);
|
||||
// r=jp.getBounds(r);
|
||||
// }
|
||||
// //BOTTOM
|
||||
// if (Math.abs(r2.y+r2.height-r.y-r.height)<MAGNET) {
|
||||
// jp.setLocation(r.x, r2.y+r2.height-r.height);
|
||||
// r=jp.getBounds(r);
|
||||
// }
|
||||
// VCENTER
|
||||
if (Math.abs(r2.y+r2.height/2-r.y-r.height/2)<MAGNET) {
|
||||
jp.setLocation(r.x, r2.y+r2.height/2-r.height/2);
|
||||
r=jp.getBounds(r);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
showBorders(jp);
|
||||
}
|
||||
|
||||
public void showBordersRight(final JCanvasPanel jp) {
|
||||
r=jp.getBounds(r);
|
||||
for (int i=0; i<CPs.size(); i++) {
|
||||
final JCanvasPanel jp2=(JCanvasPanel) CPs.get(i);
|
||||
if (!jp2.equals(jp)) {
|
||||
r2=jp2.getBounds(r2);
|
||||
jp2.hideBorder();
|
||||
// RIGHT
|
||||
if ((r2.x+r2.width-r.x-r.width)==0) {
|
||||
jp2.showBorder();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void showBorders(final JCanvasPanel jp) {
|
||||
r=jp.getBounds(r);
|
||||
for (int i=0; i<CPs.size(); i++) {
|
||||
final JCanvasPanel jp2=(JCanvasPanel) CPs.get(i);
|
||||
if (!jp2.equals(jp)) {
|
||||
r2=jp2.getBounds(r2);
|
||||
jp2.hideBorder();
|
||||
// LEFT
|
||||
if (r2.x==r.x) {
|
||||
jp2.showBorder();
|
||||
|
||||
}
|
||||
// RIGHT
|
||||
if ((r2.x+r2.width-r.x-r.width)==0) {
|
||||
jp2.showBorder();
|
||||
|
||||
}
|
||||
// //CENTER
|
||||
// if ((r2.x+r2.width/2-r.x-r.width/2)==0) {
|
||||
// jp2.showBorder();
|
||||
//
|
||||
// }
|
||||
// //TOP
|
||||
// if (r2.y==r.y) {
|
||||
// jp2.showBorder();
|
||||
// }
|
||||
// //BOTTOM
|
||||
// if ((r2.y+r2.height-r.y-r.height)==0) {
|
||||
// jp2.showBorder();
|
||||
//
|
||||
// }
|
||||
if ((r2.y+r2.height/2-r.y-r.height/2)==0) {
|
||||
jp2.showBorder();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void updateDigits() {
|
||||
for (int i=0; i<CPs.size(); i++) {
|
||||
final JCanvasPanel jp2=(JCanvasPanel) CPs.get(i);
|
||||
jp2.setVal(jp2.getVal());
|
||||
}
|
||||
}
|
||||
|
||||
public void hideBorders(final JCanvasPanel jp) {
|
||||
for (int i=0; i<CPs.size(); i++) {
|
||||
final JCanvasPanel jp2=(JCanvasPanel) CPs.get(i);
|
||||
if (!jp2.equals(jp)) {
|
||||
jp2.hideBorder();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void hideHandles(final JCanvasPanel jp) {
|
||||
for (int i=0; i<CPs.size(); i++) {
|
||||
final JCanvasPanel jp2=(JCanvasPanel) CPs.get(i);
|
||||
if (!jp2.equals(jp)) {
|
||||
jp2.hideHandle();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void showHandles(final JCanvasPanel jp) {
|
||||
hideHandles(jp);
|
||||
jp.showHandle();
|
||||
|
||||
}
|
||||
|
||||
public static boolean createControl(final ZirkelCanvas zc,
|
||||
final MouseEvent e) {
|
||||
|
||||
if (JZirkelCanvas.getCurrentJZF()==null) {
|
||||
return false;
|
||||
}
|
||||
if (JZirkelCanvas.isRestrictedSession()) {
|
||||
return false;
|
||||
}
|
||||
if (e.isPopupTrigger()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final int x=e.getX();
|
||||
final int y=e.getY();
|
||||
final JControlsManager myJCM=zc.JCM;
|
||||
|
||||
|
||||
if (PaletteManager.isSelected("ctrl_slider")) {
|
||||
myJCM.addSlider(null, x, y, 200, 29);
|
||||
return true;
|
||||
} else if (PaletteManager.isSelected("ctrl_popup")) {
|
||||
myJCM.addPopup(null, x, y, 120, 22);
|
||||
return true;
|
||||
} else if (PaletteManager.isSelected("ctrl_chkbox")) {
|
||||
myJCM.addChkBox(null, x, y, 30, 22);
|
||||
return true;
|
||||
} else if (PaletteManager.isSelected("ctrl_txtfield")) {
|
||||
myJCM.addTxtField(null, x, y, 120, 22);
|
||||
return true;
|
||||
} else if (PaletteManager.isSelected("ctrl_button")) {
|
||||
myJCM.addButton(null, x, y, 100, 22);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void removeAllControls() {
|
||||
while (CPs.size()!=0) {
|
||||
final JCanvasPanel jp=(JCanvasPanel) CPs.get(0);
|
||||
CPs.remove(jp);
|
||||
ZC.delete(jp.O);
|
||||
ZC.remove(jp);
|
||||
}
|
||||
ZC.revalidate();
|
||||
}
|
||||
|
||||
public static void removeOwnerControl(ZirkelCanvas zc,ConstructionObject o){
|
||||
if (!(o instanceof ExpressionObject)) return;
|
||||
for (int i=0; i<zc.JCM.CPs.size(); i++) {
|
||||
JCanvasPanel jp=zc.JCM.CPs.get(i);
|
||||
if (o==jp.O) {
|
||||
zc.JCM.removeControl(jp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void removeControl(final JCanvasPanel jp) {
|
||||
CPs.remove(jp);
|
||||
ZC.delete(jp.O);
|
||||
ZC.remove(jp);
|
||||
ZC.revalidate();
|
||||
}
|
||||
|
||||
public static void PrintXmlTags(final ZirkelCanvas zc, final XmlWriter xml) {
|
||||
for (int i=0; i<zc.JCM.CPs.size(); i++) {
|
||||
((JCanvasPanel) zc.JCM.CPs.get(i)).PrintXmlTags(xml);
|
||||
}
|
||||
}
|
||||
|
||||
public void addSlider(final XmlTag tag) {
|
||||
|
||||
final ExpressionObject o=(ExpressionObject) ZC.getConstruction().find(tag.getValue("Ename"));
|
||||
final JCanvasSlider jcs=addSlider(o, Integer.parseInt(tag.getValue("x")), Integer.parseInt(tag.getValue("y")), Integer.parseInt(tag.getValue("w")), Integer.parseInt(tag.getValue("h")));
|
||||
jcs.hidden=Boolean.valueOf(tag.getValue("hidden")).booleanValue();
|
||||
jcs.showcom=Boolean.valueOf(tag.getValue("showC")).booleanValue();
|
||||
jcs.showunit=Boolean.valueOf(tag.getValue("showU")).booleanValue();
|
||||
jcs.showval=Boolean.valueOf(tag.getValue("showV")).booleanValue();
|
||||
jcs.lbl_com=tag.getValue("C");
|
||||
jcs.lbl_unit=tag.getValue("U");
|
||||
// jcs.lbl_val=tag.getValue("V");
|
||||
jcs.xTICKS=Double.valueOf(tag.getValue("T")).doubleValue();
|
||||
jcs.xMIN=Double.valueOf(tag.getValue("min")).doubleValue();
|
||||
jcs.xMAX=Double.valueOf(tag.getValue("max")).doubleValue();
|
||||
|
||||
jcs.setVal(Double.valueOf(tag.getValue("V")).doubleValue());
|
||||
jcs.setTicks(jcs.xTICKS);
|
||||
|
||||
jcs.JCS.setSnapToTicks(Boolean.valueOf(tag.getValue("fixT")).booleanValue());
|
||||
jcs.JCS.setPaintTicks(Boolean.valueOf(tag.getValue("showT")).booleanValue());
|
||||
jcs.setGoodKnobPos(Double.valueOf(tag.getValue("V")).doubleValue());
|
||||
|
||||
}
|
||||
|
||||
public void addTxtField(final XmlTag tag) {
|
||||
final ExpressionObject o=(ExpressionObject) ZC.getConstruction().find(tag.getValue("Ename"));
|
||||
final JCanvasTxtfield jcs=addTxtField(o, Integer.parseInt(tag.getValue("x")), Integer.parseInt(tag.getValue("y")), Integer.parseInt(tag.getValue("w")), Integer.parseInt(tag.getValue("h")));
|
||||
jcs.hidden=Boolean.valueOf(tag.getValue("hidden")).booleanValue();
|
||||
jcs.showcom=Boolean.valueOf(tag.getValue("showC")).booleanValue();
|
||||
jcs.showunit=Boolean.valueOf(tag.getValue("showU")).booleanValue();
|
||||
jcs.showval=Boolean.valueOf(tag.getValue("showV")).booleanValue();
|
||||
jcs.lbl_com=tag.getValue("C");
|
||||
jcs.lbl_unit=tag.getValue("U");
|
||||
jcs.setVal(tag.getValue("txt"));
|
||||
jcs.JCB.setText(tag.getValue("txt"));
|
||||
}
|
||||
|
||||
public void addChkBox(final XmlTag tag) {
|
||||
final ExpressionObject o=(ExpressionObject) ZC.getConstruction().find(tag.getValue("Ename"));
|
||||
final JCanvasCheckBox jcs=addChkBox(o, Integer.parseInt(tag.getValue("x")), Integer.parseInt(tag.getValue("y")), Integer.parseInt(tag.getValue("w")), Integer.parseInt(tag.getValue("h")));
|
||||
jcs.hidden=Boolean.valueOf(tag.getValue("hidden")).booleanValue();
|
||||
jcs.showcom=Boolean.valueOf(tag.getValue("showC")).booleanValue();
|
||||
jcs.showunit=Boolean.valueOf(tag.getValue("showU")).booleanValue();
|
||||
jcs.showval=Boolean.valueOf(tag.getValue("showV")).booleanValue();
|
||||
jcs.lbl_com=tag.getValue("C");
|
||||
jcs.lbl_unit=tag.getValue("U");
|
||||
final double chked=Double.valueOf(tag.getValue("V")).doubleValue();
|
||||
jcs.setVal(chked);
|
||||
jcs.JCB.setSelected(chked==1);
|
||||
}
|
||||
|
||||
public void addButton(final XmlTag tag) {
|
||||
final ExpressionObject o=(ExpressionObject) ZC.getConstruction().find(tag.getValue("Ename"));
|
||||
final JCanvasButton jcs=addButton(o, Integer.parseInt(tag.getValue("x")), Integer.parseInt(tag.getValue("y")), Integer.parseInt(tag.getValue("w")), Integer.parseInt(tag.getValue("h")));
|
||||
jcs.hidden=Boolean.valueOf(tag.getValue("hidden")).booleanValue();
|
||||
jcs.showcom=Boolean.valueOf(tag.getValue("showC")).booleanValue();
|
||||
jcs.showunit=Boolean.valueOf(tag.getValue("showU")).booleanValue();
|
||||
jcs.showval=Boolean.valueOf(tag.getValue("showV")).booleanValue();
|
||||
jcs.lbl_com=tag.getValue("C");
|
||||
jcs.lbl_unit=tag.getValue("U");
|
||||
jcs.setVal(Double.valueOf(tag.getValue("V")).doubleValue());
|
||||
|
||||
// try {
|
||||
// // jcs.setVal(Double.valueOf(tag.getValue("V")).doubleValue());
|
||||
// jcs.setVal(o.getValue());
|
||||
// } catch (Exception ex) {
|
||||
//
|
||||
// }
|
||||
jcs.setComment(jcs.lbl_com);
|
||||
}
|
||||
|
||||
public void addPopup(final XmlTag tag) {
|
||||
final ExpressionObject o=(ExpressionObject) ZC.getConstruction().find(tag.getValue("Ename"));
|
||||
final JCanvasPopup jcs=addPopup(o, Integer.parseInt(tag.getValue("x")), Integer.parseInt(tag.getValue("y")), Integer.parseInt(tag.getValue("w")), Integer.parseInt(tag.getValue("h")));
|
||||
jcs.hidden=Boolean.valueOf(tag.getValue("hidden")).booleanValue();
|
||||
jcs.showcom=Boolean.valueOf(tag.getValue("showC")).booleanValue();
|
||||
jcs.showunit=Boolean.valueOf(tag.getValue("showU")).booleanValue();
|
||||
jcs.showval=Boolean.valueOf(tag.getValue("showV")).booleanValue();
|
||||
jcs.lbl_com=tag.getValue("C");
|
||||
jcs.lbl_unit=tag.getValue("U");
|
||||
|
||||
jcs.setItems(tag.getValue("Items").replace("@@@", "\n"));
|
||||
final double v=Double.valueOf(tag.getValue("V")).doubleValue();
|
||||
jcs.setVal(Math.round(v));
|
||||
jcs.JCB.setSelectedIndex((int) Math.round(v-1));
|
||||
// jcs.JCB.setSelected(chked==1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by new3Dwindow : localize all strings that you can find in a new
|
||||
* 3D window (floor,coordinate system...).
|
||||
*/
|
||||
public void fix3Dcomments() {
|
||||
if (CPs.size()>1) {
|
||||
JCanvasPanel jp=(JCanvasPanel) CPs.get(0);
|
||||
jp.setComment(Global.Loc("canvas.3D.floor"));
|
||||
jp=(JCanvasPanel) CPs.get(1);
|
||||
jp.setComment(Global.Loc("canvas.3D.system"));
|
||||
final TextObject t=(TextObject) ZC.getConstruction().find("Text2");
|
||||
if (t!=null) {
|
||||
t.setLines(Global.Loc("canvas.3D.rightclic"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void collectXmlTag(final XmlTag tag) {
|
||||
XmlTags.add(tag);
|
||||
}
|
||||
|
||||
public void readXmlTags() {
|
||||
for (int i=0; i<XmlTags.size(); i++) {
|
||||
final XmlTag tag=(XmlTag) XmlTags.get(i);
|
||||
if (tag.name().equals("CTRLslider")) {
|
||||
addSlider(tag);
|
||||
} else if (tag.name().equals("CTRLcheckbox")) {
|
||||
addChkBox(tag);
|
||||
} else if (tag.name().equals("CTRLbutton")) {
|
||||
addButton(tag);
|
||||
} else if (tag.name().equals("CTRLpopup")) {
|
||||
addPopup(tag);
|
||||
} else if (tag.name().equals("CTRLtxtfield")) {
|
||||
addTxtField(tag);
|
||||
}
|
||||
}
|
||||
if (XmlTags.size()>0) {
|
||||
XmlTags.clear();
|
||||
hideHandles(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
296
eric/controls/SliderSnap.java
Normal file
296
eric/controls/SliderSnap.java
Normal file
|
|
@ -0,0 +1,296 @@
|
|||
/*
|
||||
|
||||
Copyright 2006 Eric Hakenholz
|
||||
|
||||
This file is part of C.a.R. software.
|
||||
|
||||
C.a.R. is a free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, version 3 of the License.
|
||||
|
||||
C.a.R. is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
package eric.controls;
|
||||
|
||||
import java.awt.EventQueue;
|
||||
import java.awt.event.HierarchyEvent;
|
||||
import java.awt.event.HierarchyListener;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseMotionListener;
|
||||
import java.beans.PropertyChangeEvent;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JSlider;
|
||||
import javax.swing.UIDefaults;
|
||||
import javax.swing.UIManager;
|
||||
import javax.swing.event.MouseInputAdapter;
|
||||
import javax.swing.plaf.ComponentUI;
|
||||
import javax.swing.plaf.basic.BasicSliderUI;
|
||||
|
||||
public class SliderSnap extends BasicSliderUI {
|
||||
/**
|
||||
* The UI class implements the current slider Look and Feel.
|
||||
*/
|
||||
private static Class sliderClass;
|
||||
private static Method xForVal, yForVal;
|
||||
private static ReinitListener reinitListener = new ReinitListener();
|
||||
|
||||
public SliderSnap() {
|
||||
super(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the UI as normal, but intercepts the call, so a listener can be
|
||||
* attached.
|
||||
*/
|
||||
public static ComponentUI createUI(final JComponent c) {
|
||||
if (c == null || sliderClass == null)
|
||||
return null;
|
||||
final UIDefaults defaults = UIManager.getLookAndFeelDefaults();
|
||||
try {
|
||||
Method m = (Method) defaults.get(sliderClass);
|
||||
if (m == null) {
|
||||
m = sliderClass.getMethod("createUI",
|
||||
new Class[] { JComponent.class });
|
||||
defaults.put(sliderClass, m);
|
||||
}
|
||||
final ComponentUI uiObject = (ComponentUI) m.invoke(null,
|
||||
new Object[] { c });
|
||||
if (uiObject instanceof BasicSliderUI)
|
||||
c.addHierarchyListener(new MouseAttacher());
|
||||
return uiObject;
|
||||
} catch (final Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void init() {
|
||||
// check we don't initialise twice
|
||||
if (sliderClass != null)
|
||||
return;
|
||||
final Init init = new Init();
|
||||
if (EventQueue.isDispatchThread()) {
|
||||
init.run();
|
||||
} else {
|
||||
// This code must run on the EDT for data visibility
|
||||
try {
|
||||
EventQueue.invokeAndWait(init);
|
||||
} catch (final Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Listeners for when the JSlider becomes visible then attaches the mouse
|
||||
* listeners, then removes itself.
|
||||
*/
|
||||
private static class MouseAttacher implements HierarchyListener {
|
||||
public void hierarchyChanged(final HierarchyEvent evt) {
|
||||
final long flags = evt.getChangeFlags();
|
||||
if ((flags & HierarchyEvent.DISPLAYABILITY_CHANGED) > 0
|
||||
&& evt.getComponent() instanceof JSlider) {
|
||||
final JSlider c = (JSlider) evt.getComponent();
|
||||
c.removeHierarchyListener(this);
|
||||
attachTo(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Listens for Look and Feel changes and re-initialises the class.
|
||||
*/
|
||||
private static class ReinitListener implements PropertyChangeListener {
|
||||
public void propertyChange(final PropertyChangeEvent evt) {
|
||||
if ("lookAndFeel".equals(evt.getPropertyName())) {
|
||||
// The look and feel was changed so we need to re-insert
|
||||
// Our hook into the new UIDefaults map
|
||||
sliderClass = null;
|
||||
xForVal = yForVal = null;
|
||||
UIManager.removePropertyChangeListener(reinitListener);
|
||||
init();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialises the reflective methods and adjusts the current Look and Feel.
|
||||
*/
|
||||
private static class Init implements Runnable {
|
||||
public void run() {
|
||||
try {
|
||||
final UIDefaults defaults = UIManager.getLookAndFeelDefaults();
|
||||
sliderClass = defaults.getUIClass("SliderUI");
|
||||
// Set up two reflective method calls
|
||||
xForVal = BasicSliderUI.class.getDeclaredMethod(
|
||||
"xPositionForValue", new Class[] { int.class });
|
||||
yForVal = BasicSliderUI.class.getDeclaredMethod(
|
||||
"yPositionForValue", new Class[] { int.class });
|
||||
// Allow us access to the methods
|
||||
xForVal.setAccessible(true);
|
||||
yForVal.setAccessible(true);
|
||||
// Replace UI class with ourselves
|
||||
defaults.put("SliderUI", SliderSnap.class.getName());
|
||||
UIManager.addPropertyChangeListener(reinitListener);
|
||||
} catch (final Exception e) {
|
||||
sliderClass = null;
|
||||
xForVal = yForVal = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called to attach mouse listeners to the JSlider.
|
||||
*/
|
||||
private static void attachTo(final JSlider c) {
|
||||
final MouseMotionListener[] listeners = c.getMouseMotionListeners();
|
||||
for (final MouseMotionListener m : listeners) {
|
||||
if (m instanceof TrackListener) {
|
||||
c.removeMouseMotionListener(m); // remove original
|
||||
final SnapListener listen = new SnapListener(m,
|
||||
(BasicSliderUI) c.getUI(), c);
|
||||
c.addMouseMotionListener(listen);
|
||||
c.addMouseListener(listen);
|
||||
c.addPropertyChangeListener("UI", listen);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class SnapListener extends MouseInputAdapter implements
|
||||
PropertyChangeListener {
|
||||
private final MouseMotionListener delegate;
|
||||
/**
|
||||
* Original Look and Feel implementation
|
||||
*/
|
||||
private final BasicSliderUI ui;
|
||||
/**
|
||||
* Our slider
|
||||
*/
|
||||
private final JSlider slider;
|
||||
/**
|
||||
* Offset of mouse click from centre of slider thumb
|
||||
*/
|
||||
private int offset;
|
||||
|
||||
public SnapListener(final MouseMotionListener delegate,
|
||||
final BasicSliderUI ui, final JSlider slider) {
|
||||
this.delegate = delegate;
|
||||
this.ui = ui;
|
||||
this.slider = slider;
|
||||
}
|
||||
|
||||
/**
|
||||
* UI can change at any point, so we need to listen for these events.
|
||||
*/
|
||||
public void propertyChange(final PropertyChangeEvent evt) {
|
||||
if ("UI".equals(evt.getPropertyName())) {
|
||||
// Remove old listeners and create new ones
|
||||
slider.removeMouseMotionListener(this);
|
||||
slider.removeMouseListener(this);
|
||||
slider.removePropertyChangeListener("UI", this);
|
||||
attachTo(slider);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements the actual "snap while dragging" behaviour. If snap to
|
||||
* ticks is enabled on this slider, then the location for the nearest
|
||||
* tick/label is calculated and the click location is translated before
|
||||
* being passed to the delegate.
|
||||
*/
|
||||
@Override
|
||||
public void mouseDragged(final MouseEvent evt) {
|
||||
if (slider.getSnapToTicks()) { // if we are set to snap
|
||||
final int pos = getLocationForValue(getSnappedValue(evt));
|
||||
// if above call fails and returns -1, take no action
|
||||
if (pos > -1) {
|
||||
if (slider.getOrientation() == JSlider.HORIZONTAL)
|
||||
evt.translatePoint(pos - evt.getX() + offset, 0);
|
||||
else
|
||||
evt.translatePoint(0, pos - evt.getY() + offset);
|
||||
}
|
||||
}
|
||||
delegate.mouseDragged(evt);
|
||||
}
|
||||
|
||||
/**
|
||||
* When the slider is clicked we need to record the offset from thumb
|
||||
* center.
|
||||
*/
|
||||
@Override
|
||||
public void mousePressed(final MouseEvent evt) {
|
||||
|
||||
final int pos = (slider.getOrientation() == JSlider.HORIZONTAL) ? evt
|
||||
.getX()
|
||||
: evt.getY();
|
||||
final int loc = getLocationForValue(getSnappedValue(evt));
|
||||
this.offset = (loc < 0) ? 0 : pos - loc;
|
||||
}
|
||||
|
||||
/* Pass straight to delegate. */
|
||||
@Override
|
||||
public void mouseMoved(final MouseEvent evt) {
|
||||
delegate.mouseMoved(evt);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the nearest snapable value given a MouseEvent. Code
|
||||
* adapted from BasicSliderUI.
|
||||
*/
|
||||
public int getSnappedValue(final MouseEvent evt) {
|
||||
final int value = slider.getOrientation() == JSlider.HORIZONTAL ? ui
|
||||
.valueForXPosition(evt.getX())
|
||||
: ui.valueForYPosition(evt.getY());
|
||||
// Now calculate if we should adjust the value
|
||||
int snappedValue = value;
|
||||
int tickSpacing = 0;
|
||||
final int majorTickSpacing = slider.getMajorTickSpacing();
|
||||
final int minorTickSpacing = slider.getMinorTickSpacing();
|
||||
if (minorTickSpacing > 0)
|
||||
tickSpacing = minorTickSpacing;
|
||||
else if (majorTickSpacing > 0)
|
||||
tickSpacing = majorTickSpacing;
|
||||
// If it's not on a tick, change the value
|
||||
if (tickSpacing != 0) {
|
||||
if ((value - slider.getMinimum()) % tickSpacing != 0) {
|
||||
final float temp = (float) (value - slider.getMinimum())
|
||||
/ (float) tickSpacing;
|
||||
snappedValue = slider.getMinimum()
|
||||
+ (Math.round(temp) * tickSpacing);
|
||||
}
|
||||
}
|
||||
return snappedValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides the x or y co-ordinate for a slider value, depending on
|
||||
* orientation.
|
||||
*/
|
||||
public int getLocationForValue(final int value) {
|
||||
try {
|
||||
// Reflectively call slider ui code
|
||||
final Method m = slider.getOrientation() == JSlider.HORIZONTAL ? xForVal
|
||||
: yForVal;
|
||||
final Integer result = (Integer) m.invoke(ui,
|
||||
new Object[] { new Integer(value) });
|
||||
return result.intValue();
|
||||
} catch (final InvocationTargetException e) {
|
||||
return -1;
|
||||
} catch (final IllegalAccessException e) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue