Simple single-color 8x8x8 LED Cube with AVRs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Frame.java 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. /*
  2. * Frame.java
  3. *
  4. *
  5. * Copyright 2011 Thomas Buck <xythobuz@me.com>
  6. * Copyright 2011 Max Nuding <max.nuding@gmail.com>
  7. * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
  8. *
  9. * This file is part of LED-Cube.
  10. *
  11. * LED-Cube is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * LED-Cube is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. import com.sun.j3d.utils.universe.*;
  25. import javax.media.j3d.*;
  26. import java.awt.*;
  27. import java.awt.event.*;
  28. import javax.swing.*;
  29. import javax.swing.event.*;
  30. import java.io.File;
  31. public class Frame extends JFrame implements ListSelectionListener {
  32. // Anfang Variablen
  33. private GraphicsConfiguration gConfig = SimpleUniverse.getPreferredConfiguration();
  34. private Canvas3D cubeCanvas = new Canvas3D(gConfig);
  35. private Led3D ledView = new Led3D(cubeCanvas);
  36. // Anfang Attribute
  37. private JButton editA = new JButton();
  38. private JButton editB = new JButton();
  39. private JButton editC = new JButton();
  40. private JButton editD = new JButton();
  41. private JButton editE = new JButton();
  42. private JButton editF = new JButton();
  43. private JButton editG = new JButton();
  44. private JButton editH = new JButton();
  45. private DefaultListModel frameListModel = new DefaultListModel();
  46. public JList frameList = new JList();
  47. private JScrollPane frameListScrollPane = new JScrollPane(frameList);
  48. private JButton frameUp = new JButton();
  49. private JButton frameDown = new JButton();
  50. private JButton frameAdd = new JButton();
  51. private JButton frameRemove = new JButton();
  52. private JButton frameRename = new JButton();
  53. private JButton Frame = new JButton();
  54. private JList animList = new JList();
  55. private DefaultListModel animModel = new DefaultListModel();
  56. private JScrollPane animScrollPane = new JScrollPane(animList);
  57. private JButton animUp = new JButton();
  58. private JButton animDown = new JButton();
  59. private JButton animAdd = new JButton();
  60. private JButton animRemove = new JButton();
  61. private JButton animRename = new JButton();
  62. private JTextField animPath = new JTextField();
  63. private JButton load = new JButton();
  64. private JButton save = new JButton();
  65. private JButton saveAs = new JButton();
  66. private JComboBox jComboBox1 = new JComboBox();
  67. private JButton upload = new JButton();
  68. private JButton download = new JButton();
  69. private JLabel jLabel4 = new JLabel();
  70. private JTextField frameRemaining = new JTextField();
  71. private JLabel frameLengthLabel = new JLabel();
  72. private JTextField frameLengthText = new JTextField();
  73. private JButton frameDuration = new JButton();
  74. // Ende Attribute
  75. public cubeWorker worker = new cubeWorker();
  76. private boolean fileSelected = false;
  77. // Ende Variablen
  78. private int saveExitDialog() {
  79. String[] Optionen = {"Yes", "No"};
  80. int Auswahl = JOptionPane.showOptionDialog(this, "Do you want to save your changes?", "Save?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, Optionen, Optionen[0]);
  81. if (Auswahl == JOptionPane.YES_OPTION) {
  82. return 1;
  83. } else {
  84. return 0;
  85. }
  86. }
  87. private String askString(String title, String text) {
  88. return JOptionPane.showInputDialog(null, text, title, JOptionPane.QUESTION_MESSAGE);
  89. }
  90. private void errorMessage(String s) {
  91. String[] Optionen = {"OK"};
  92. JOptionPane.showOptionDialog(this, s, "Error!", JOptionPane.YES_OPTION, JOptionPane.ERROR_MESSAGE, null, Optionen, Optionen[0]);
  93. }
  94. public void valueChanged(ListSelectionEvent evt) {
  95. if ((!evt.getValueIsAdjusting()) && ((evt.getSource() == animList) || (evt.getSource() == frameList))) {
  96. // If animList or framsList is the source, we act...
  97. // If both selections are valid, update Frame duration and set 3D data
  98. if ((animList.getSelectedIndex() != -1) && (frameList.getSelectedIndex() != -1)) {
  99. ledView.setData(worker.getFrame(animList.getSelectedIndex(), frameList.getSelectedIndex()));
  100. frameLengthText.setText(Integer.toString(worker.getFrameTime(animList.getSelectedIndex(), frameList.getSelectedIndex())));
  101. } else {
  102. // clear Frame duration
  103. frameLengthText.setText("");
  104. }
  105. if ((evt.getSource() == animList) && (animList.getSelectedIndex() != -1)) {
  106. // animList selection changed, update frameList
  107. frameListModel.clear();
  108. for (int i = 0; i < worker.numOfFrames(animList.getSelectedIndex()); i++) {
  109. frameListModel.addElement(worker.getFrameName(animList.getSelectedIndex(), i));
  110. }
  111. frameList.setModel(frameListModel);
  112. }
  113. }
  114. }
  115. private void save() {
  116. if (fileSelected == false) {
  117. JFileChooser fc = new JFileChooser();
  118. int ret = fc.showSaveDialog(this);
  119. if (ret == JFileChooser.APPROVE_OPTION) {
  120. File file = fc.getSelectedFile();
  121. fileSelected = true;
  122. animPath.setText(file.getPath());
  123. worker.saveState(animPath.getText());
  124. }
  125. } else {
  126. worker.saveState(animPath.getText());
  127. }
  128. }
  129. public Frame(String title) {
  130. // Frame-Initialisierung
  131. super(title);
  132. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  133. String[] sPorts = worker.getSerialPorts();
  134. for(int i = 0; i < sPorts.length; i++){
  135. jComboBox1.addItem(sPorts[i]);
  136. }
  137. for(int i = 0; i < worker.numOfAnimations(); i++){
  138. animModel.addElement(worker.getAnimationName(i));
  139. }
  140. addWindowListener(new WindowAdapter() {
  141. public void windowClosing(WindowEvent evt) {
  142. if (worker.changedStateSinceSave()) {
  143. if (saveExitDialog() == 1) {
  144. save();
  145. } else {
  146. return;
  147. }
  148. }
  149. System.exit(0);
  150. }
  151. });
  152. int frameWidth = 661;
  153. int frameHeight = 417;
  154. setSize(frameWidth, frameHeight);
  155. Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  156. int x = (d.width - getSize().width) / 2;
  157. int y = (d.height - getSize().height) / 2 ;
  158. setLocation(x, y);
  159. Container cp = getContentPane();
  160. cp.setLayout(null);
  161. // Anfang Komponenten
  162. //----- 3D-----
  163. //-------------
  164. cubeCanvas.setBounds(8, 8, 250, 250);
  165. cp.add(cubeCanvas);
  166. //-------------
  167. editA.setBounds(264, 8, 107, 25);
  168. editA.setText("Layer A");
  169. editA.setFont(new Font("Dialog", Font.PLAIN, 13));
  170. cp.add(editA);
  171. editA.addActionListener(new ActionListener() {
  172. public void actionPerformed(ActionEvent evt) {
  173. editA_ActionPerformed(evt);
  174. }
  175. });
  176. editB.setBounds(264, 40, 107, 25);
  177. editB.setText("Layer B");
  178. editB.setFont(new Font("Dialog", Font.PLAIN, 13));
  179. cp.add(editB);
  180. editB.addActionListener(new ActionListener() {
  181. public void actionPerformed(ActionEvent evt) {
  182. editB_ActionPerformed(evt);
  183. }
  184. });
  185. editC.setBounds(264, 72, 107, 25);
  186. editC.setText("Layer C");
  187. editC.setFont(new Font("Dialog", Font.PLAIN, 13));
  188. cp.add(editC);
  189. editC.addActionListener(new ActionListener() {
  190. public void actionPerformed(ActionEvent evt) {
  191. editC_ActionPerformed(evt);
  192. }
  193. });
  194. editD.setBounds(264, 104, 107, 25);
  195. editD.setText("Layer D");
  196. editD.setFont(new Font("Dialog", Font.PLAIN, 13));
  197. cp.add(editD);
  198. editD.addActionListener(new ActionListener() {
  199. public void actionPerformed(ActionEvent evt) {
  200. editD_ActionPerformed(evt);
  201. }
  202. });
  203. editE.setBounds(264, 136, 107, 25);
  204. editE.setText("Layer E");
  205. editE.setFont(new Font("Dialog", Font.PLAIN, 13));
  206. cp.add(editE);
  207. editE.addActionListener(new ActionListener() {
  208. public void actionPerformed(ActionEvent evt) {
  209. editE_ActionPerformed(evt);
  210. }
  211. });
  212. editF.setBounds(264, 168, 107, 25);
  213. editF.setText("Layer F");
  214. editF.setFont(new Font("Dialog", Font.PLAIN, 13));
  215. cp.add(editF);
  216. editF.addActionListener(new ActionListener() {
  217. public void actionPerformed(ActionEvent evt) {
  218. editF_ActionPerformed(evt);
  219. }
  220. });
  221. editG.setBounds(264, 200, 107, 25);
  222. editG.setText("Layer G");
  223. editG.setFont(new Font("Dialog", Font.PLAIN, 13));
  224. cp.add(editG);
  225. editG.addActionListener(new ActionListener() {
  226. public void actionPerformed(ActionEvent evt) {
  227. editG_ActionPerformed(evt);
  228. }
  229. });
  230. editH.setBounds(264, 232, 107, 25);
  231. editH.setText("Layer H");
  232. editH.setFont(new Font("Dialog", Font.PLAIN, 13));
  233. cp.add(editH);
  234. editH.addActionListener(new ActionListener() {
  235. public void actionPerformed(ActionEvent evt) {
  236. editH_ActionPerformed(evt);
  237. }
  238. });
  239. frameListScrollPane.setBounds(384, 8, 145, 249);
  240. frameList.setModel(frameListModel);
  241. cp.add(frameListScrollPane);
  242. frameUp.setBounds(544, 8, 107, 28);
  243. frameUp.setText("Move up");
  244. frameUp.setFont(new Font("Dialog", Font.PLAIN, 13));
  245. cp.add(frameUp);
  246. frameUp.addActionListener(new ActionListener() {
  247. public void actionPerformed(ActionEvent evt) {
  248. frameUp_ActionPerformed(evt);
  249. }
  250. });
  251. frameAdd.setBounds(544, 39, 107, 28);
  252. frameAdd.setText("Add");
  253. frameAdd.setFont(new Font("Dialog", Font.PLAIN, 13));
  254. cp.add(frameAdd);
  255. frameAdd.addActionListener(new ActionListener() {
  256. public void actionPerformed(ActionEvent evt) {
  257. frameAdd_ActionPerformed(evt);
  258. }
  259. });
  260. frameRemove.setBounds(544, 70, 107, 28);
  261. frameRemove.setText("Remove");
  262. frameRemove.setFont(new Font("Dialog", Font.PLAIN, 13));
  263. cp.add(frameRemove);
  264. frameRemove.addActionListener(new ActionListener() {
  265. public void actionPerformed(ActionEvent evt) {
  266. frameRemove_ActionPerformed(evt);
  267. }
  268. });
  269. frameRename.setBounds(544, 101, 107, 28);
  270. frameRename.setText("Rename");
  271. frameRename.setFont(new Font("Dialog", Font.PLAIN, 13));
  272. cp.add(frameRename);
  273. frameRename.addActionListener(new ActionListener() {
  274. public void actionPerformed(ActionEvent evt) {
  275. int a = animList.getSelectedIndex();
  276. if (a < 0) {
  277. errorMessage("Select an animation!");
  278. return;
  279. }
  280. int f = frameList.getSelectedIndex();
  281. if (f < 0) {
  282. errorMessage("Select a Frame!");
  283. return;
  284. }
  285. worker.setFrameName(askString("Rename", "Rename " + frameList.getSelectedValue() + "?"), a, f);
  286. frameListModel.set(f, worker.getFrameName(a, f));
  287. frameList.setModel(frameListModel);
  288. }
  289. });
  290. frameDown.setBounds(544, 132, 107, 28);
  291. frameDown.setText("Move down");
  292. frameDown.setFont(new Font("Dialog", Font.PLAIN, 13));
  293. cp.add(frameDown);
  294. frameDown.addActionListener(new ActionListener() {
  295. public void actionPerformed(ActionEvent evt) {
  296. frameDown_ActionPerformed(evt);
  297. }
  298. });
  299. frameLengthLabel.setBounds(536, 160, 113, 24);
  300. frameLengthLabel.setText("Time (1/24 sec)");
  301. frameLengthLabel.setFont(new Font("Dialog", Font.PLAIN, 13));
  302. cp.add(frameLengthLabel);
  303. frameLengthText.setBounds(536, 184, 50, 24);
  304. frameLengthText.setText("");
  305. frameLengthText.setFont(new Font("Dialog", Font.PLAIN, 13));
  306. cp.add(frameLengthText);
  307. frameDuration.setBounds(590, 184, 60, 24);
  308. frameDuration.setText("OK");
  309. frameDuration.setFont(new Font("Dialog", Font.PLAIN, 13));
  310. cp.add(frameDuration);
  311. frameDuration.addActionListener(new ActionListener() {
  312. public void actionPerformed(ActionEvent evt) {
  313. if (frameLengthText.getText().equals("0")) {
  314. errorMessage("0 is not a valid value!");
  315. frameLengthText.setText("1");
  316. } else if (Integer.parseInt(frameLengthText.getText()) > 256) {
  317. errorMessage("Value too high. Max: 256");
  318. frameLengthText.setText("256");
  319. return;
  320. } else {
  321. if (animList.getSelectedIndex() == -1) {
  322. errorMessage("Please select an animation!");
  323. return;
  324. }
  325. if (frameList.getSelectedIndex() == -1) {
  326. errorMessage("Please select a Frame!");
  327. return;
  328. }
  329. worker.setFrameTime((byte)(Integer.parseInt(frameLengthText.getText()) - 1), animList.getSelectedIndex(), frameList.getSelectedIndex());
  330. }
  331. }
  332. });
  333. animScrollPane.setBounds(8, 264, 209, 121);
  334. animList.setModel(animModel);
  335. cp.add(animScrollPane);
  336. animUp.setBounds(224, 264, 99, 25);
  337. animUp.setText("Move up");
  338. animUp.setFont(new Font("Dialog", Font.PLAIN, 13));
  339. cp.add(animUp);
  340. animUp.addActionListener(new ActionListener() {
  341. public void actionPerformed(ActionEvent evt) {
  342. animUp_ActionPerformed(evt);
  343. }
  344. });
  345. animDown.setBounds(224, 368, 99, 25);
  346. animDown.setText("Move down");
  347. animDown.setFont(new Font("Dialog", Font.PLAIN, 13));
  348. cp.add(animDown);
  349. animDown.addActionListener(new ActionListener() {
  350. public void actionPerformed(ActionEvent evt) {
  351. animDown_ActionPerformed(evt);
  352. }
  353. });
  354. animRename.setBounds(224, 342, 99, 25);
  355. animRename.setText("Rename");
  356. animRename.setFont(new Font("Dialog", Font.PLAIN, 13));
  357. cp.add(animRename);
  358. animRename.addActionListener(new ActionListener() {
  359. public void actionPerformed(ActionEvent evt) {
  360. int a = animList.getSelectedIndex();
  361. if (a < 0) {
  362. errorMessage("Select an animation!");
  363. return;
  364. }
  365. worker.setAnimationName(askString("Rename", "Rename " + animList.getSelectedValue() + "?"), a);
  366. animModel.set(a, worker.getAnimationName(a));
  367. animList.setModel(animModel);
  368. }
  369. });
  370. animAdd.setBounds(224, 290, 99, 25);
  371. animAdd.setText("Add");
  372. animAdd.setFont(new Font("Dialog", Font.PLAIN, 13));
  373. cp.add(animAdd);
  374. animAdd.addActionListener(new ActionListener() {
  375. public void actionPerformed(ActionEvent evt) {
  376. animAdd_ActionPerformed(evt);
  377. }
  378. });
  379. animRemove.setBounds(224, 316, 99, 25);
  380. animRemove.setText("Remove");
  381. animRemove.setFont(new Font("Dialog", Font.PLAIN, 13));
  382. cp.add(animRemove);
  383. animRemove.addActionListener(new ActionListener() {
  384. public void actionPerformed(ActionEvent evt) {
  385. animRemove_ActionPerformed(evt);
  386. }
  387. });
  388. animPath.setBounds(344, 264, 305, 24);
  389. animPath.setEditable(false);
  390. animPath.setText("Load/Save an animation file...");
  391. animPath.setFont(new Font("Dialog", Font.PLAIN, 13));
  392. cp.add(animPath);
  393. load.setBounds(344, 296, 100, 25);
  394. load.setText("Load");
  395. load.setFont(new Font("Dialog", Font.PLAIN, 13));
  396. cp.add(load);
  397. load.addActionListener(new ActionListener() {
  398. public void actionPerformed(ActionEvent evt) {
  399. load_ActionPerformed(evt);
  400. }
  401. });
  402. save.setBounds(454, 296, 100, 25);
  403. save.setText("Save");
  404. save.setFont(new Font("Dialog", Font.PLAIN, 13));
  405. cp.add(save);
  406. save.addActionListener(new ActionListener() {
  407. public void actionPerformed(ActionEvent evt) {
  408. save_ActionPerformed(evt);
  409. }
  410. });
  411. saveAs.setBounds(564, 296, 90, 25);
  412. saveAs.setText("Save As");
  413. saveAs.setFont(new Font("Dialog", Font.PLAIN, 13));
  414. cp.add(saveAs);
  415. saveAs.addActionListener(new ActionListener() {
  416. public void actionPerformed(ActionEvent evt) {
  417. saveAs_ActionPerformed(evt);
  418. }
  419. });
  420. jComboBox1.setBounds(344, 328, 305, 24);
  421. jComboBox1.setFont(new Font("Dialog", Font.PLAIN, 13));
  422. cp.add(jComboBox1);
  423. upload.setBounds(344, 360, 147, 25);
  424. upload.setText("Upload");
  425. upload.setFont(new Font("Dialog", Font.PLAIN, 13));
  426. cp.add(upload);
  427. upload.addActionListener(new ActionListener() {
  428. public void actionPerformed(ActionEvent evt) {
  429. upload_ActionPerformed(evt);
  430. }
  431. });
  432. download.setBounds(504, 360, 147, 25);
  433. download.setText("Download");
  434. download.setFont(new Font("Dialog", Font.PLAIN, 13));
  435. cp.add(download);
  436. download.addActionListener(new ActionListener() {
  437. public void actionPerformed(ActionEvent evt) {
  438. download_ActionPerformed(evt);
  439. }
  440. });
  441. jLabel4.setBounds(536, 208, 112, 20);
  442. jLabel4.setText("Remaining:");
  443. jLabel4.setFont(new Font("Dialog", Font.PLAIN, 13));
  444. cp.add(jLabel4);
  445. frameRemaining.setBounds(536, 232, 113, 24);
  446. frameRemaining.setEditable(false);
  447. frameRemaining.setText(String.valueOf(worker.framesRemaining()));
  448. frameRemaining.setFont(new Font("Dialog", Font.PLAIN, 13));
  449. cp.add(frameRemaining);
  450. animList.setFont(new Font("Dialog", Font.PLAIN, 13));
  451. frameList.setFont(new Font("Dialog", Font.PLAIN, 13));
  452. // Ende Komponenten
  453. animList.addListSelectionListener(this);
  454. frameList.addListSelectionListener(this);
  455. setResizable(false);
  456. setVisible(true);
  457. }
  458. // Anfang Methoden
  459. // Anfang Ereignisprozeduren
  460. public void editA_ActionPerformed(ActionEvent evt) {
  461. if(animList.getSelectedIndex() == -1){
  462. errorMessage("Please select an animation.");
  463. } else if(frameList.getSelectedIndex() == -1){
  464. errorMessage("Please select a Frame.");
  465. } else {
  466. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), 0, worker, this);
  467. }
  468. }
  469. public void editB_ActionPerformed(ActionEvent evt) {
  470. if(animList.getSelectedIndex() == -1){
  471. errorMessage("Please select an animation.");
  472. } else if(frameList.getSelectedIndex() == -1){
  473. errorMessage("Please select a Frame.");
  474. } else {
  475. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), 1, worker, this);
  476. }
  477. }
  478. public void editC_ActionPerformed(ActionEvent evt) {
  479. if(animList.getSelectedIndex() == -1){
  480. errorMessage("Please select an animation.");
  481. } else if(frameList.getSelectedIndex() == -1){
  482. errorMessage("Please select a Frame.");
  483. } else {
  484. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), 2, worker, this);
  485. }
  486. }
  487. public void editD_ActionPerformed(ActionEvent evt) {
  488. if(animList.getSelectedIndex() == -1){
  489. errorMessage("Please select an animation.");
  490. } else if(frameList.getSelectedIndex() == -1){
  491. errorMessage("Please select a Frame.");
  492. } else {
  493. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), 3, worker, this);
  494. }
  495. }
  496. public void editE_ActionPerformed(ActionEvent evt) {
  497. if(animList.getSelectedIndex() == -1){
  498. errorMessage("Please select an animation.");
  499. } else if(frameList.getSelectedIndex() == -1){
  500. errorMessage("Please select a Frame.");
  501. } else {
  502. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), 4, worker, this);
  503. }
  504. }
  505. public void editF_ActionPerformed(ActionEvent evt) {
  506. if(animList.getSelectedIndex() == -1){
  507. errorMessage("Please select an animation.");
  508. } else if(frameList.getSelectedIndex() == -1){
  509. errorMessage("Please select a Frame.");
  510. } else {
  511. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), 5, worker, this);
  512. }
  513. }
  514. public void editG_ActionPerformed(ActionEvent evt) {
  515. if(animList.getSelectedIndex() == -1){
  516. errorMessage("Please select an animation.");
  517. } else if(frameList.getSelectedIndex() == -1){
  518. errorMessage("Please select a Frame.");
  519. } else {
  520. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), 6, worker, this);
  521. }
  522. }
  523. public void editH_ActionPerformed(ActionEvent evt) {
  524. if(animList.getSelectedIndex() == -1){
  525. errorMessage("Please select an animation.");
  526. } else if(frameList.getSelectedIndex() == -1){
  527. errorMessage("Please select a Frame.");
  528. } else {
  529. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), 7, worker, this);
  530. }
  531. }
  532. public void frameUp_ActionPerformed(ActionEvent evt) {
  533. int i = frameList.getSelectedIndex();
  534. if ((i > 0) && (frameListModel.getSize() >= 2)) {
  535. Object tmp = frameListModel.get(i);
  536. frameListModel.set(i, frameListModel.get(i - 1));
  537. frameListModel.set(i - 1, tmp);
  538. frameList.setSelectedIndex(i - 1);
  539. worker.moveFrame(worker.UP, animList.getSelectedIndex(), frameList.getSelectedIndex());
  540. }
  541. }
  542. public void frameDown_ActionPerformed(ActionEvent evt) {
  543. int i = frameList.getSelectedIndex();
  544. if ((i >= 0) && (frameListModel.getSize() >= 2) && (i < (frameListModel.getSize() - 1))) {
  545. Object tmp = frameListModel.get(i);
  546. frameListModel.set(i, frameListModel.get(i + 1));
  547. frameListModel.set(i + 1, tmp);
  548. frameList.setSelectedIndex(i + 1);
  549. worker.moveFrame(worker.DOWN, animList.getSelectedIndex(), frameList.getSelectedIndex());
  550. }
  551. }
  552. public void frameAdd_ActionPerformed(ActionEvent evt) {
  553. if(animList.getSelectedIndex() == -1){
  554. errorMessage("Please select an animation!");
  555. } else {
  556. worker.addFrame(animList.getSelectedIndex());
  557. frameRemaining.setText(Integer.toString(worker.framesRemaining()));
  558. int n = worker.numOfFrames(animList.getSelectedIndex()) - 1;
  559. if (n < 0) {
  560. n = 0;
  561. }
  562. frameListModel.add(n, worker.getFrameName(animList.getSelectedIndex(), n));
  563. frameList.setModel(frameListModel);
  564. }
  565. }
  566. public void frameRemove_ActionPerformed(ActionEvent evt) {
  567. if(animList.getSelectedIndex() == -1){
  568. errorMessage("Select an animation.");
  569. } else if(frameList.getSelectedIndex() == -1){
  570. errorMessage("Select a Frame.");
  571. } else {
  572. worker.removeFrame(animList.getSelectedIndex(), frameList.getSelectedIndex());
  573. frameRemaining.setText(Integer.toString(worker.framesRemaining()));
  574. frameListModel.removeElementAt(frameList.getSelectedIndex());
  575. frameList.setModel(frameListModel);
  576. }
  577. }
  578. public void animUp_ActionPerformed(ActionEvent evt) {
  579. int i = animList.getSelectedIndex();
  580. if ((i > 0) && (animModel.getSize() >= 2)) {
  581. Object tmp = animModel.get(i);
  582. animModel.set(i, animModel.get(i - 1));
  583. animModel.set(i - 1, tmp);
  584. animList.setSelectedIndex(i - 1);
  585. worker.moveAnimation(worker.UP, animList.getSelectedIndex());
  586. }
  587. }
  588. public void animDown_ActionPerformed(ActionEvent evt) {
  589. int i = animList.getSelectedIndex();
  590. if ((i >= 0) && (animModel.getSize() >= 2) && (i < (animModel.getSize() - 1))) {
  591. Object tmp = animModel.get(i);
  592. animModel.set(i, animModel.get(i + 1));
  593. animModel.set(i + 1, tmp);
  594. animList.setSelectedIndex(i + 1);
  595. worker.moveAnimation(worker.DOWN, animList.getSelectedIndex());
  596. }
  597. }
  598. public void animAdd_ActionPerformed(ActionEvent evt) {
  599. if(worker.addAnimation() == -1){
  600. errorMessage("Could not add animation!");
  601. } else {
  602. animModel.clear();
  603. for (int i = 0; i < worker.numOfAnimations(); i++) {
  604. animModel.add(i, worker.getAnimationName(i));
  605. }
  606. animList.setModel(animModel);
  607. }
  608. }
  609. public void animRemove_ActionPerformed(ActionEvent evt) {
  610. if(animList.getSelectedIndex() == -1){
  611. errorMessage("Select an animation.");
  612. } else {
  613. worker.removeAnimation(animList.getSelectedIndex());
  614. animModel.removeElementAt(animList.getSelectedIndex());
  615. animList.setModel(animModel);
  616. }
  617. }
  618. public void load_ActionPerformed(ActionEvent evt) {
  619. JFileChooser fc = new JFileChooser();
  620. int ret = fc.showOpenDialog(this);
  621. if (ret == JFileChooser.APPROVE_OPTION) {
  622. File file = fc.getSelectedFile();
  623. if (fileSelected == false) {
  624. fileSelected = true;
  625. }
  626. animPath.setText(file.getPath());
  627. worker.loadState(animPath.getText());
  628. animModel.clear();
  629. for (int i = 0; i < worker.numOfAnimations(); i++) {
  630. animModel.addElement(worker.getAnimationName(i));
  631. }
  632. animList.setModel(animModel);
  633. frameListModel.clear();
  634. frameList.setModel(frameListModel);
  635. }
  636. }
  637. public void save_ActionPerformed(ActionEvent evt) {
  638. if (fileSelected == false) {
  639. JFileChooser fc = new JFileChooser();
  640. int ret = fc.showSaveDialog(this);
  641. if (ret == JFileChooser.APPROVE_OPTION) {
  642. File file = fc.getSelectedFile();
  643. fileSelected = true;
  644. animPath.setText(file.getPath());
  645. worker.saveState(animPath.getText());
  646. }
  647. } else {
  648. worker.saveState(animPath.getText());
  649. }
  650. }
  651. public void saveAs_ActionPerformed(ActionEvent evt) {
  652. JFileChooser fc;
  653. if (fileSelected == true) {
  654. fc = new JFileChooser(new File(animPath.getText()).getParentFile());
  655. } else {
  656. fc = new JFileChooser();
  657. }
  658. int ret = fc.showSaveDialog(this);
  659. if (ret == JFileChooser.APPROVE_OPTION) {
  660. File file = fc.getSelectedFile();
  661. if (fileSelected == false) {
  662. fileSelected = true;
  663. }
  664. animPath.setText(file.getPath());
  665. worker.saveState(animPath.getText());
  666. }
  667. }
  668. public void upload_ActionPerformed(ActionEvent evt) {
  669. if (jComboBox1.getSelectedItem().equals("Select serial port...")) {
  670. errorMessage("No serial port selected...");
  671. } else {
  672. if (worker.probeCubeConnected((String)jComboBox1.getSelectedItem())) {
  673. if (worker.uploadState((String)jComboBox1.getSelectedItem()) != 0) {
  674. errorMessage("Could not upload data!");
  675. }
  676. } else {
  677. errorMessage("Cube does not respond...");
  678. }
  679. }
  680. }
  681. public void download_ActionPerformed(ActionEvent evt) {
  682. if (jComboBox1.getSelectedItem().equals("Select serial port...")) {
  683. errorMessage("No serial port selected...");
  684. } else {
  685. worker.downloadState((String)jComboBox1.getSelectedItem());
  686. }
  687. }
  688. public Led3D get3D() {
  689. return ledView;
  690. }
  691. public static void main(String[] args) {
  692. Frame f = new Frame("Cube Control");
  693. Led3D l = f.get3D();
  694. java.util.Scanner sc = new java.util.Scanner(System.in);
  695. System.out.println("#### Cube Control Debug Console ####");
  696. System.out.println("## Enter a Command ('h' for help) ##");
  697. System.out.print("$> ");
  698. do {
  699. if (sc.hasNextLine()) {
  700. String s = sc.nextLine();
  701. if (s.equals("p") || (s.equals("print")))
  702. l.printTranslationData();
  703. if (s.equals("q") || s.equals("quit"))
  704. System.exit(0);
  705. if (s.equals("on") || s.equals("1")) {
  706. short[] d = new short[64];
  707. for (int i = 0; i < d.length; i++) {
  708. d[i] = 0xFF;
  709. }
  710. l.setData(d);
  711. System.out.println("All LEDs on now...");
  712. }
  713. if (s.equals("off") || s.equals("0")) {
  714. short[] d = new short[64];
  715. for (int i = 0; i < d.length; i++) {
  716. d[i] = 0x00;
  717. }
  718. l.setData(d);
  719. System.out.println("All LEDs off now...");
  720. }
  721. if (s.equals("e") || s.equals("exec")) {
  722. System.out.println(HelperUtility.runHelper(new String[0]));
  723. }
  724. if (s.startsWith("e ") || s.startsWith("exec ")) {
  725. int pos = 0;
  726. while (s.charAt(pos) != ' ') {
  727. pos++;
  728. }
  729. String[] arr = new String[1];
  730. arr[0] = s.substring(pos + 1);
  731. System.out.println(HelperUtility.runHelper(arr));
  732. }
  733. if (s.equals("r") || s.equals("reset")) {
  734. l.resetView();
  735. }
  736. if (s.equals("h") || (s.equals("help"))) {
  737. System.out.println("Commands:");
  738. System.out.println("\t'on' / '1'\t:\tToggle all LEDs on");
  739. System.out.println("\t'off' / '0'\t:\tToggle all LEDs off");
  740. System.out.println("\t'print' / 'p'\t:\tPrint 3D Translation Matrix Data");
  741. System.out.println("\t'exec' / 'e'\t:\tExecute helper with given args");
  742. System.out.println("\t'reset' / 'r'\t:\tReset rotation of cube");
  743. System.out.println("\t'help' / 'h'\t:\tShow this message");
  744. System.out.println("\t'quit' / 'q'\t:\tExit Cube Control");
  745. }
  746. System.out.print("$> ");
  747. }
  748. } while (true);
  749. }
  750. }