Make first real commit: copy of CaRMetal 4.2.8

This commit is contained in:
Glen Whitney 2018-09-04 22:51:42 -04:00
parent 002acfc88e
commit c312811084
1120 changed files with 226843 additions and 1 deletions

View file

@ -0,0 +1,45 @@
package eric.jobs;
import java.awt.BorderLayout;
import java.awt.Graphics;
import javax.swing.JComponent;
/**
* A wrapper Container for holding components that use a background Color
* containing an alpha value with some transparency.
*
* A Component that uses a transparent background should really have its
* opaque property set to false so that the area it occupies is first painted
* by its opaque ancestor (to make sure no painting artifacts exist). However,
* if the property is set to false, then most Swing components will not paint
* the background at all, so you lose the transparent background Color.
*
* This components attempts to get around this problem by doing the
* background painting on behalf of its contained Component, using the
* background Color of the Component.
*/
public class AlphaContainer extends JComponent
{
private JComponent component;
public AlphaContainer(JComponent component)
{
this.component = component;
setLayout( new BorderLayout() );
setOpaque( false );
component.setOpaque( false );
add( component );
}
/**
* Paint the background using the background Color of the
* contained component
*/
@Override
public void paintComponent(Graphics g)
{
g.setColor( component.getBackground() );
g.fillRect(0, 0, getWidth(), getHeight());
super.paintComponent(g);
}
}

225
eric/jobs/Base64Coder.java Normal file
View file

@ -0,0 +1,225 @@
// Copyright 2003-2010 Christian d'Heureuse, Inventec Informatik AG, Zurich, Switzerland
// www.source-code.biz, www.inventec.ch/chdh
//
// This module is multi-licensed and may be used under the terms
// of any of the following licenses:
//
// EPL, Eclipse Public License, http://www.eclipse.org/legal
// LGPL, GNU Lesser General Public License, http://www.gnu.org/licenses/lgpl.html
// AL, Apache License, http://www.apache.org/licenses
// BSD, BSD License, http://www.opensource.org/licenses/bsd-license.php
//
// Please contact the author if you need another license.
// This module is provided "as is", without warranties of any kind.
package eric.jobs;
/**
* A Base64 encoder/decoder.
*
* <p>
* This class is used to encode and decode data in Base64 format as described in RFC 1521.
*
* <p>
* Project home page: <a href="http://www.source-code.biz/base64coder/java/">www.source-code.biz/base64coder/java</a><br>
* Author: Christian d'Heureuse, Inventec Informatik AG, Zurich, Switzerland<br>
* Multi-licensed: EPL / LGPL / AL / BSD.
*/
public class Base64Coder {
// The line separator string of the operating system.
private static final String systemLineSeparator = System.getProperty("line.separator");
// Mapping table from 6-bit nibbles to Base64 characters.
private static char[] map1 = new char[64];
static {
int i=0;
for (char c='A'; c<='Z'; c++) map1[i++] = c;
for (char c='a'; c<='z'; c++) map1[i++] = c;
for (char c='0'; c<='9'; c++) map1[i++] = c;
map1[i++] = '+'; map1[i++] = '/'; }
// Mapping table from Base64 characters to 6-bit nibbles.
private static byte[] map2 = new byte[128];
static {
for (int i=0; i<map2.length; i++) map2[i] = -1;
for (int i=0; i<64; i++) map2[map1[i]] = (byte)i; }
/**
* Encodes a string into Base64 format.
* No blanks or line breaks are inserted.
* @param s A String to be encoded.
* @return A String containing the Base64 encoded data.
*/
public static String encodeString (String s) {
return new String(encode(s.getBytes())); }
/**
* Encodes a byte array into Base 64 format and breaks the output into lines of 76 characters.
* This method is compatible with <code>sun.misc.BASE64Encoder.encodeBuffer(byte[])</code>.
* @param in An array containing the data bytes to be encoded.
* @return A String containing the Base64 encoded data, broken into lines.
*/
public static String encodeLines (byte[] in) {
return encodeLines(in, 0, in.length, 76, systemLineSeparator); }
/**
* Encodes a byte array into Base 64 format and breaks the output into lines.
* @param in An array containing the data bytes to be encoded.
* @param iOff Offset of the first byte in <code>in</code> to be processed.
* @param iLen Number of bytes to be processed in <code>in</code>, starting at <code>iOff</code>.
* @param lineLen Line length for the output data. Should be a multiple of 4.
* @param lineSeparator The line separator to be used to separate the output lines.
* @return A String containing the Base64 encoded data, broken into lines.
*/
public static String encodeLines (byte[] in, int iOff, int iLen, int lineLen, String lineSeparator) {
int blockLen = (lineLen*3) / 4;
if (blockLen <= 0) throw new IllegalArgumentException();
int lines = (iLen+blockLen-1) / blockLen;
int bufLen = ((iLen+2)/3)*4 + lines*lineSeparator.length();
StringBuilder buf = new StringBuilder(bufLen);
int ip = 0;
while (ip < iLen) {
int l = Math.min(iLen-ip, blockLen);
buf.append (encode(in, iOff+ip, l));
buf.append (lineSeparator);
ip += l; }
return buf.toString(); }
/**
* Encodes a byte array into Base64 format.
* No blanks or line breaks are inserted in the output.
* @param in An array containing the data bytes to be encoded.
* @return A character array containing the Base64 encoded data.
*/
public static char[] encode (byte[] in) {
return encode(in, 0, in.length); }
/**
* Encodes a byte array into Base64 format.
* No blanks or line breaks are inserted in the output.
* @param in An array containing the data bytes to be encoded.
* @param iLen Number of bytes to process in <code>in</code>.
* @return A character array containing the Base64 encoded data.
*/
public static char[] encode (byte[] in, int iLen) {
return encode(in, 0, iLen); }
/**
* Encodes a byte array into Base64 format.
* No blanks or line breaks are inserted in the output.
* @param in An array containing the data bytes to be encoded.
* @param iOff Offset of the first byte in <code>in</code> to be processed.
* @param iLen Number of bytes to process in <code>in</code>, starting at <code>iOff</code>.
* @return A character array containing the Base64 encoded data.
*/
public static char[] encode (byte[] in, int iOff, int iLen) {
int oDataLen = (iLen*4+2)/3; // output length without padding
int oLen = ((iLen+2)/3)*4; // output length including padding
char[] out = new char[oLen];
int ip = iOff;
int iEnd = iOff + iLen;
int op = 0;
while (ip < iEnd) {
int i0 = in[ip++] & 0xff;
int i1 = ip < iEnd ? in[ip++] & 0xff : 0;
int i2 = ip < iEnd ? in[ip++] & 0xff : 0;
int o0 = i0 >>> 2;
int o1 = ((i0 & 3) << 4) | (i1 >>> 4);
int o2 = ((i1 & 0xf) << 2) | (i2 >>> 6);
int o3 = i2 & 0x3F;
out[op++] = map1[o0];
out[op++] = map1[o1];
out[op] = op < oDataLen ? map1[o2] : '='; op++;
out[op] = op < oDataLen ? map1[o3] : '='; op++; }
return out; }
/**
* Decodes a string from Base64 format.
* No blanks or line breaks are allowed within the Base64 encoded input data.
* @param s A Base64 String to be decoded.
* @return A String containing the decoded data.
* @throws IllegalArgumentException If the input is not valid Base64 encoded data.
*/
public static String decodeString (String s) {
return new String(decode(s)); }
/**
* Decodes a byte array from Base64 format and ignores line separators, tabs and blanks.
* CR, LF, Tab and Space characters are ignored in the input data.
* This method is compatible with <code>sun.misc.BASE64Decoder.decodeBuffer(String)</code>.
* @param s A Base64 String to be decoded.
* @return An array containing the decoded data bytes.
* @throws IllegalArgumentException If the input is not valid Base64 encoded data.
*/
public static byte[] decodeLines (String s) {
char[] buf = new char[s.length()];
int p = 0;
for (int ip = 0; ip < s.length(); ip++) {
char c = s.charAt(ip);
if (c != ' ' && c != '\r' && c != '\n' && c != '\t')
buf[p++] = c; }
return decode(buf, 0, p); }
/**
* Decodes a byte array from Base64 format.
* No blanks or line breaks are allowed within the Base64 encoded input data.
* @param s A Base64 String to be decoded.
* @return An array containing the decoded data bytes.
* @throws IllegalArgumentException If the input is not valid Base64 encoded data.
*/
public static byte[] decode (String s) {
return decode(s.toCharArray()); }
/**
* Decodes a byte array from Base64 format.
* No blanks or line breaks are allowed within the Base64 encoded input data.
* @param in A character array containing the Base64 encoded data.
* @return An array containing the decoded data bytes.
* @throws IllegalArgumentException If the input is not valid Base64 encoded data.
*/
public static byte[] decode (char[] in) {
return decode(in, 0, in.length); }
/**
* Decodes a byte array from Base64 format.
* No blanks or line breaks are allowed within the Base64 encoded input data.
* @param in A character array containing the Base64 encoded data.
* @param iOff Offset of the first character in <code>in</code> to be processed.
* @param iLen Number of characters to process in <code>in</code>, starting at <code>iOff</code>.
* @return An array containing the decoded data bytes.
* @throws IllegalArgumentException If the input is not valid Base64 encoded data.
*/
public static byte[] decode (char[] in, int iOff, int iLen) {
if (iLen%4 != 0) throw new IllegalArgumentException ("Length of Base64 encoded input string is not a multiple of 4.");
while (iLen > 0 && in[iOff+iLen-1] == '=') iLen--;
int oLen = (iLen*3) / 4;
byte[] out = new byte[oLen];
int ip = iOff;
int iEnd = iOff + iLen;
int op = 0;
while (ip < iEnd) {
int i0 = in[ip++];
int i1 = in[ip++];
int i2 = ip < iEnd ? in[ip++] : 'A';
int i3 = ip < iEnd ? in[ip++] : 'A';
if (i0 > 127 || i1 > 127 || i2 > 127 || i3 > 127)
throw new IllegalArgumentException ("Illegal character in Base64 encoded data.");
int b0 = map2[i0];
int b1 = map2[i1];
int b2 = map2[i2];
int b3 = map2[i3];
if (b0 < 0 || b1 < 0 || b2 < 0 || b3 < 0)
throw new IllegalArgumentException ("Illegal character in Base64 encoded data.");
int o0 = ( b0 <<2) | (b1>>>4);
int o1 = ((b1 & 0xf)<<4) | (b2>>>2);
int o2 = ((b2 & 3)<<6) | b3;
out[op++] = (byte)o0;
if (op<oLen) out[op++] = (byte)o1;
if (op<oLen) out[op++] = (byte)o2; }
return out; }
// Dummy constructor.
private Base64Coder() {}
} // end class Base64Coder

View file

@ -0,0 +1,122 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package eric.jobs;
import eric.GUI.ZDialog.ZButton;
import eric.GUI.ZDialog.ZCheckBox;
import eric.GUI.ZDialog.ZDialog;
import eric.GUI.ZDialog.ZTextFieldAndLabel;
import java.awt.event.KeyEvent;
import rene.gui.Global;
/**
*
* @author erichake
*/
public class JobControlPanel extends ZDialog {
private ZButton createBTN, removeBTN;
private ZCheckBox hideFinalBox,staticJobBox;
private ZTextFieldAndLabel okMessage, failedMessage, targetslist;
private JobManager MAN;
public JobControlPanel(JobManager man,int x,int y,int w,int h) {
super(Global.Loc("job.gui.title"), x,y,w,h,true,true);
MAN=man;
createBTN=new ZButton(Global.Loc("job.gui.close")) {
@Override
public void action() {
MAN.hideControlDialog(true);
}
};
removeBTN=new ZButton(Global.Loc("job.gui.delete")) {
@Override
public void action() {
MAN.hideControlDialog(false);
}
};
okMessage=new ZTextFieldAndLabel(Global.Loc("job.gui.ok"), man.getMessage_ok(), LWIDTH,CHEIGHT) {
@Override
public void actionKey(KeyEvent k) {
MAN.setMessage_ok(okMessage.getText());
}
};
failedMessage=new ZTextFieldAndLabel(Global.Loc("job.gui.failed"), man.getMessage_failed(), LWIDTH,CHEIGHT) {
@Override
public void actionKey(KeyEvent k) {
MAN.setMessage_failed(failedMessage.getText());
}
};
targetslist=new ZTextFieldAndLabel(Global.Loc("job.gui.targets"), man.getTargetNames(), 0,CHEIGHT) {
@Override
public void actionMouse() {
MAN.setJobTool();
}
@Override
public void actionKey(KeyEvent k) {
MAN.setTargets(targetslist.getText());
}
};
hideFinalBox=new ZCheckBox(Global.Loc("job.gui.hidebox"), man.isHidefinals()) {
@Override
public void action() {
MAN.setHidefinals(hideFinalBox.isSelected());
}
};
staticJobBox=new ZCheckBox(Global.Loc("job.gui.staticjob"), man.isStaticJob()) {
@Override
public void action() {
MAN.setStaticJob(staticJobBox.isSelected());
}
};
// add(title);
//
add(createBTN);
add(removeBTN);
add(staticJobBox);
add(hideFinalBox);
add(okMessage);
add(failedMessage);
add(targetslist);
fixComponents();
}
@Override
public void fixComponents() {
targetslist.setBounds(MARGINW, MARGINTOP1, CWIDTH, CHEIGHT);
okMessage.setBounds(MARGINW, MARGINTOP3, CWIDTH, CHEIGHT);
failedMessage.setBounds(MARGINW, MARGINTOP4, CWIDTH, CHEIGHT);
hideFinalBox.setBounds(MARGINW, MARGINTOP2, CWIDTH, CHEIGHT);
staticJobBox.setBounds(D_WIDTH-MARGINW-CWIDTH+LWIDTH, MARGINTOP2, CWIDTH, CHEIGHT);
createBTN.setBounds(D_WIDTH-BWIDTH-MARGINW, MARGINTOP5, BWIDTH, CHEIGHT);
removeBTN.setBounds(D_WIDTH-MARGINW-CWIDTH+LWIDTH, MARGINTOP5, BWIDTH, CHEIGHT);
}
@Override
public void doClose() {
MAN.cancelControlDialog();
}
public String getTargetslist() {
return targetslist.getText();
}
public void setTargetslist(String targets) {
targetslist.setText(targets);
}
}

322
eric/jobs/JobManager.java Normal file
View file

@ -0,0 +1,322 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package eric.jobs;
import eric.FileTools;
import eric.GUI.palette.PaletteManager;
import eric.GUI.pipe_tools;
import java.util.ArrayList;
import java.util.Enumeration;
import rene.gui.Global;
import rene.util.xml.XmlWriter;
import rene.zirkel.ZirkelCanvas;
import rene.zirkel.objects.ConstructionObject;
import rene.zirkel.tools.DefineJobTool;
/**
*
* @author erichake
*/
public class JobManager {
ZirkelCanvas ZC;
private String target_names=null; // Only for loading process...
private ArrayList<ConstructionObject> targets=new ArrayList<ConstructionObject>();
private String backup=null;
private String message_ok=Global.getParameter("job.message.ok", Global.Loc("job.message.ok"));
private String message_failed=Global.getParameter("job.message.failed", Global.Loc("job.message.failed"));
private boolean hidefinals=false;
private boolean staticjob=false;
private JobControlPanel controlPanel=null;
private JobValidPanel validPanel=null;
private boolean printExerciseArguments=true;
private int ctrlX=3,ctrlY=3,ctrlW=374,ctrlH=162;
private int validX,validY,validW=550,validH=36;
public JobManager(ZirkelCanvas zc) {
ZC=zc;
}
public void backup() {
try {
printExerciseArguments=false; // protect from recursively get Exercise arguments
String file=FileTools.getCurrentFileSource();
printExerciseArguments=true;
// Compress the byte representation of the file :
byte[] b=StringCompressionUtils.Compress(file.getBytes());
// then save it into base64 format :
backup=new String(Base64Coder.encode(b));
} catch (Exception ex) {
System.out.println("backup error");
}
}
public void restore() {
if (backup!=null) {
try {
String targets_backup=getTargetNames();
// decode the base64 representation of the compressed file :
byte[] b=Base64Coder.decode(backup);
// decompress the file :
byte[] b1=StringCompressionUtils.Decompress(b);
// restore the construction :
FileTools.setCurrentFileSource(new String(b1));
setTargets(targets_backup);
setTargetsColor(true);
setHiddenToSuperHidden();
} catch (Exception ex) {
}
}
}
public ArrayList<ConstructionObject> getTargets() {
return targets;
}
public String getTargetNames() {
String names="";
for (int i=0; i<targets.size(); i++) {
names+=";"+targets.get(i).getName();
}
return names.replaceFirst(";", "");
}
/* Two methods only for loading process :
*
*/
public void setTargetNames(String t) {
target_names=t;
}
public void setTargets() {
if (target_names!=null) {
setTargets(target_names);
target_names=null;
setTargetsColor(true);
addValidPanel();
}
}
public void setTargets(String t) {
setTargetSelected(false);
targets.clear();
String[] names=t.split(";");
for (int i=0; i<names.length; i++) {
ConstructionObject o=ZC.getConstruction().find(names[i]);
if (o!=null) {
targets.add(o);
}
}
setTargetSelected(true);
}
public void addTarget(ConstructionObject o) {
targets.add(o);
}
public void removeTarget(ConstructionObject o) {
targets.remove(o);
}
public void setTargetSelected(boolean sel) {
for (int i=0; i<targets.size(); i++) {
targets.get(i).setSelected(sel);
}
ZC.repaint();
}
public void setHiddenToSuperHidden() {
Enumeration e=ZC.getConstruction().elements();
while (e.hasMoreElements()) {
ConstructionObject c=(ConstructionObject) e.nextElement();
if (c.isHidden(true)) {
c.setSuperHidden(true);
}
}
ZC.reloadCD();
}
public void setSuperHiddenToHidden() {
Enumeration e=ZC.getConstruction().elements();
while (e.hasMoreElements()) {
ConstructionObject c=(ConstructionObject) e.nextElement();
if (c.isSuperHidden(true)) {
c.setSuperHidden(false);
c.setHidden(true);
}
}
ZC.reloadCD();
}
public void setTargetsColor(boolean select) {
Enumeration e=ZC.getConstruction().elements();
while (e.hasMoreElements()) {
ConstructionObject c=(ConstructionObject) e.nextElement();
c.setJobTarget(false);
}
for (int i=0; i<targets.size(); i++) {
targets.get(i).setSuperHidden(false);
}
if (select) {
for (int i=0; i<targets.size(); i++) {
targets.get(i).setJobTarget(true);
targets.get(i).setSuperHidden(hidefinals);
}
}
ZC.repaint();
}
/**
* @return the message_ok
*/
public String getMessage_ok() {
return message_ok;
}
public void setMessage_ok(String mess_ok) {
message_ok=mess_ok;
Global.setParameter("job.message.ok", message_ok);
}
public String getMessage_failed() {
return message_failed;
}
public void setMessage_failed(String mess_failed) {
message_failed=mess_failed;
Global.setParameter("job.message.failed", message_failed);
}
public boolean isStaticJob() {
return staticjob;
}
public void setStaticJob(boolean b){
staticjob=b;
}
public boolean isHidefinals() {
return hidefinals;
}
public void setHidefinals(boolean hidef) {
hidefinals=hidef;
}
public void setBackup(String s) {
backup=s;
}
public void printArgs(final XmlWriter xml) {
if ((targets.size()>0)&&(printExerciseArguments)) {
xml.startTagStart("Exercise");
xml.printArg("message_ok", message_ok);
xml.printArg("message_failed", message_failed);
xml.printArg("hidefinals", String.valueOf(hidefinals));
xml.printArg("staticjob", String.valueOf(staticjob));
xml.printArg("targets", getTargetNames());
xml.printArg("backup", backup);
xml.finishTagNewLine();
}
}
/*******************************
* GUI PART :
*******************************/
public void init() {
if (controlPanel!=null) {
controlPanel.init();
} else if (validPanel!=null) {
validPanel.init(ZC.getSize().width, ZC.getSize().height);
}
}
public void addControlPanel() {
removeControlPanel();
controlPanel=new JobControlPanel(this,ctrlX,ctrlY,ctrlW,ctrlH);
ZC.add(controlPanel,0);
ZC.repaint();
init();
}
public void removeControlPanel() {
if (controlPanel!=null) {
ctrlX=controlPanel.getLocation().x;
ctrlY=controlPanel.getLocation().y;
ZC.remove(controlPanel);
controlPanel=null;
ZC.repaint();
}
}
public void addValidPanel() {
removeValidPanel();
if (targets.size()>0) {
validPanel=new JobValidPanel(this,0,0,validW,validH);
ZC.add(validPanel,0);
ZC.repaint();
init();
}
}
public void removeValidPanel() {
if (validPanel!=null) {
ZC.remove(validPanel);
validPanel=null;
ZC.repaint();
}
}
public void cancelControlDialog(){
hideControlDialog(false);
}
public void hideControlDialog(boolean createJob) {
setTargetSelected(false);
removeControlPanel();
pipe_tools.getContent().requestFocus();
if (createJob&&targets.size()>0) {
backup();
addValidPanel();
setHiddenToSuperHidden();
} else {
targets.clear();
backup=null;
setSuperHiddenToHidden();
}
setTargetsColor(true);
PaletteManager.ClicOn("point");
}
public void showControlDialog() {
removeValidPanel();
restore();
setTargetsColor(false);
setSuperHiddenToHidden();
addControlPanel();
setTargetsField();
setJobTool();
}
public void setJobTool() {
setTargetSelected(true);
PaletteManager.deselectgeomgroup();
ZC.setTool(new DefineJobTool());
}
public void setTargetsField() {
if (controlPanel!=null) {
controlPanel.setTargetslist(getTargetNames());
}
}
public void validate() {
JobValidation v=new JobValidation(ZC);
v.checkAllsteps();
}
}

54
eric/jobs/JobMessage.java Normal file
View file

@ -0,0 +1,54 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package eric.jobs;
import eric.JZirkelCanvas;
import java.awt.Dimension;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import eric.JEricPanel;
import rene.zirkel.ZirkelCanvas;
/**
*
* @author erichake
*/
public class JobMessage {
static private int D_WIDTH=300;
static private int D_HEIGHT=70;
public static void showMessage(String m) {
showMessage(m, D_WIDTH, D_HEIGHT);
}
public static void showMessage(String m, int w, int h) {
ZirkelCanvas zc=JZirkelCanvas.getCurrentZC();
if (zc!=null) {
JOptionPane.showMessageDialog(zc, getPanel("<html>"+m+"</html>", w, h));
}
}
private static JEricPanel getPanel(String m, int w, int h) {
JEricPanel jp=new JEricPanel();
jp.setLayout(new BoxLayout(jp, BoxLayout.X_AXIS));
jp.setOpaque(false);
JLabel label=new JLabel(m);
jp.add(label);
fixsize(jp, w, h);
return jp;
}
private static void fixsize(JComponent jc, int w, int h) {
Dimension d=new Dimension(w, h);
jc.setSize(d);
jc.setMaximumSize(d);
jc.setMinimumSize(d);
jc.setPreferredSize(d);
}
}

View file

@ -0,0 +1,112 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package eric.jobs;
import eric.GUI.ZDialog.ZButton;
import eric.GUI.ZDialog.ZDialog;
import eric.GUI.ZDialog.ZLabel;
import eric.GUI.ZDialog.ZTools;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import eric.JEricPanel;
import rene.gui.Global;
/**
*
* @author erichake
*/
public class JobValidPanel extends ZDialog {
private JobManager MAN;
private ZButton validBTN, restoreBTN;
private ZLabel label;
protected int BWIDTH=180; // Button width
public JobValidPanel(JobManager man,int x,int y,int w,int h) {
super("", x,y,w,h,false,false);
MAN=man;
setLayout(null);
validBTN=new ZButton(Global.Loc("job.gui.valid")) {
@Override
public void action() {
MAN.validate();
}
};
restoreBTN=new ZButton(Global.Loc("job.gui.restore")) {
@Override
public void action() {
MAN.restore();
}
};
label=new ZLabel(Global.Loc("job.gui.exercise"));
add(label);
add(validBTN);
add(restoreBTN);
increaseFonts(1);
fixComponents();
}
public void increaseFonts(int size){
for (int i=0;i<getComponentCount();i++){
Component c=getComponent(i);
Font f=c.getFont();
Font nf=new Font(f.getFontName(),f.getStyle(),f.getSize()+size);
c.setFont(nf);
}
}
public void fixComponents() {
label.setBounds(MARGINW, MARGINTOP1, CWIDTH, CHEIGHT);
validBTN.setBounds(D_WIDTH-MARGINW-BWIDTH, MARGINTOP1, BWIDTH, CHEIGHT);
restoreBTN.setBounds(D_WIDTH-2*MARGINW-2*BWIDTH, MARGINTOP1, BWIDTH, CHEIGHT);
revalidate();
}
public void init(int w, int h) {
int x=(w-D_WIDTH)/2;
int y=h-D_HEIGHT-4;
setBounds(x, y, D_WIDTH, D_HEIGHT);
fixComponents();
}
private class myButton extends JButton {
public myButton(String lbl) {
super(lbl);
setFont(new Font(Global.GlobalFont, 0, 12));
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
action();
}
});
}
public void action() {
}
}
private class myLabel extends JLabel {
public myLabel(String lbl) {
super(lbl);
setFont(new Font(Global.GlobalFont, 0, 12));
}
}
}

View file

@ -0,0 +1,209 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package eric.jobs;
import eric.GUI.pipe_tools;
import eric.Progress_Bar;
import java.util.ArrayList;
import java.util.Enumeration;
import rene.gui.Global;
import rene.zirkel.ZirkelCanvas;
import rene.zirkel.construction.Construction;
import rene.zirkel.objects.ConstructionObject;
import rene.zirkel.objects.PointObject;
/**
*
* @author erichake
*/
public class JobValidation {
private final int MAX=1000;
private ZirkelCanvas ZC;
private Construction C;
private ArrayList<PointObject> mobiles=new ArrayList<PointObject>();
private ArrayList<double[]> mobilesCoords=new ArrayList<double[]>();
private ArrayList<ConstructionObject> targets, clones;
public JobValidation(ZirkelCanvas zc) {
ZC=zc;
C=ZC.getConstruction();
initMobiles();
targets=ZC.job_getTargets();
}
public void initMobiles() {
mobiles.clear();
mobilesCoords.clear();
Enumeration e=C.elements();
while (e.hasMoreElements()) {
final ConstructionObject c=(ConstructionObject) e.nextElement();
if (c instanceof PointObject) {
PointObject pt=(PointObject) c;
if (pt.moveable()&&pt.insidewindow()) {
mobiles.add(pt);
double[] tab={pt.getX(), pt.getY()};
mobilesCoords.add(tab);
}
}
}
}
/**
* Find every clones of targets object in the whole construction
* @return true if everything is fine. return false in case of
* error.
*/
public boolean initClones() {
clones=new ArrayList<ConstructionObject>();
for (int i=0; i<targets.size(); i++) {
ConstructionObject o=targets.get(i);
Enumeration e=C.elements();
while (e.hasMoreElements()) {
ConstructionObject c=(ConstructionObject) e.nextElement();
if ((o!=c)&&(!c.isHidden())) {
/* Careful ! "equals" is a method of ConstructionObjects classes :
* It says if both objects have same type and are at the same places.
*/
if (o.equals(c)) {
clones.add(c);
break;
}
}
}
}
return (targets.size()==clones.size());
}
public void reset() {
// Reset position for non magnetic mobile objects :
for (int i=0; i<mobiles.size(); i++) {
double[] tab=mobilesCoords.get(i);
mobiles.get(i).move(tab[0], tab[1]);
}
ZC.validate();
}
public boolean checkHorizontalAligment() {
double inc=2*C.getW()/(mobiles.size()+2);
double left=C.getX()-C.getW()+inc;
for (int i=0; i<mobiles.size(); i++) {
PointObject pt=mobiles.get(i);
pt.move(left, 0);
pt.validate();
left+=inc;
}
ZC.validate();
return isTargetsEqualClones();
}
public boolean check1step() {
for (int i=0; i<mobiles.size(); i++) {
PointObject pt=mobiles.get(i);
pt.alea();
}
ZC.validate();
return isTargetsEqualClones();
}
public boolean isTargetsEqualClones() {
for (int i=0; i<targets.size(); i++) {
if ((targets.get(i).valid())||(clones.get(i).valid())) {
if (!targets.get(i).equals(clones.get(i))) {
return false;
}
}
}
return true;
}
public void dynamicConstructionFailed() {
reset();
String message="<b>"+ZC.job_getMessageFailed();
message+="</b><br>("+Global.Loc("restrict.failed.initial")+")";
if (!pipe_tools.Exercise_To_HTML(false, ZC.job_getMessageFailed())) {
JobMessage.showMessage(message);
}
}
public void dynamicConstructionFailed(int failed) {
reset();
double d=100.0*failed/(MAX+1);
String message="<b>"+ZC.job_getMessageFailed();
message+="</b><br>("+Global.Loc("restrict.failed.percent")+String.valueOf(Math.round(d))+"%)";
if (!pipe_tools.Exercise_To_HTML(false, ZC.job_getMessageFailed())) {
JobMessage.showMessage(message);
}
}
public void staticConstructionFailed() {
reset();
String message="<b>"+ZC.job_getMessageFailed()+"</b>";
if (!pipe_tools.Exercise_To_HTML(false, ZC.job_getMessageFailed())) {
JobMessage.showMessage(message);
}
}
public void constructionOk() {
reset();
if (!pipe_tools.Exercise_To_HTML(true, ZC.job_getMessageOk())) {
JobMessage.showMessage(ZC.job_getMessageOk());
}
}
public void constructionOkExceptAlignment() {
reset();
String message="<b>"+ZC.job_getMessageOk();
message+="</b><br>("+Global.Loc("job.gui.alignment")+")";
if (!pipe_tools.Exercise_To_HTML(true, ZC.job_getMessageOk()+" ("+Global.Loc("job.gui.alignment")+")")) {
JobMessage.showMessage(message);
}
}
public void justSee() {
ZC.paint(ZC.getGraphics());
try {
Thread.sleep(200);
} catch (InterruptedException ex) {
}
}
public void checkAllsteps() {
if ((mobiles.size()>0)&&(!ZC.job_isStaticJob())) {
// if it's a dynamic exercise :
int failed=0;
if (!initClones()) {
dynamicConstructionFailed();
return;
}
int progress=0;
Progress_Bar.create(Global.Loc("job.gui.progressmessage"), 0, MAX-1);
for (int i=0; i<MAX; i++) {
if (!check1step()) {
failed++;
}
Progress_Bar.nextValue();
}
Progress_Bar.close();
if (failed>0) {
dynamicConstructionFailed(failed);
return;
}
if (!checkHorizontalAligment()) {
constructionOkExceptAlignment();
return;
}
constructionOk();
} else {
// if it's a static exercise :
if (!initClones()) {
staticConstructionFailed();
return;
}
constructionOk();
}
}
}

View file

@ -0,0 +1,80 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package eric.jobs;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
/**
*
* @author erichake
*/
public class StringCompressionUtils {
/**
* Compress data.
* @param bytesToCompress is the byte array to compress.
* @return a compressed byte array.
* @throws java.io.IOException
*/
public static byte[] Compress(byte[] bytesToCompress) throws IOException {
// Compressor with highest level of compression.
Deflater compressor=new Deflater(Deflater.BEST_COMPRESSION);
compressor.setInput(bytesToCompress); // Give the compressor the data to compress.
compressor.finish();
// Create an expandable byte array to hold the compressed data.
// It is not necessary that the compressed data will be smaller than
// the uncompressed data.
ByteArrayOutputStream bos=new ByteArrayOutputStream(bytesToCompress.length);
// Compress the data
byte[] buf=new byte[bytesToCompress.length+100];
while (!compressor.finished()) {
bos.write(buf, 0, compressor.deflate(buf));
}
bos.close();
// Get the compressed data
return bos.toByteArray();
}
/**
* Decompress data.
* @param compressedBytes is the compressed byte array.
* @return decompressed byte array.
* @throws java.io.IOException
* @throws java.util.zip.DataFormatException
*/
public static byte[] Decompress(byte[] compressedBytes) throws IOException, DataFormatException {
// Initialize decompressor.
Inflater decompressor=new Inflater();
decompressor.setInput(compressedBytes); // Give the decompressor the data to decompress.
decompressor.finished();
// Create an expandable byte array to hold the decompressed data.
// It is not necessary that the decompressed data will be larger than
// the compressed data.
ByteArrayOutputStream bos=new ByteArrayOutputStream(compressedBytes.length);
// Decompress the data
byte[] buf=new byte[compressedBytes.length+100];
while (!decompressor.finished()) {
bos.write(buf, 0, decompressor.inflate(buf));
}
bos.close();
// Get the decompressed data.
return bos.toByteArray();
}
}