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

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