Java tutorial
/* * The MIT License (MIT) * * Copyright (c) 2013 Dries K. Aka Dries007 and the CCM modding crew. * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package ccm.pay2spawn.types.guis; import ccm.pay2spawn.configurator.Configurator; import ccm.pay2spawn.network.TestMessage; import com.google.common.base.Strings; import com.google.gson.JsonObject; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.HashMap; import static ccm.pay2spawn.types.RandomItemType.*; import static ccm.pay2spawn.util.Constants.*; /** * @author Dries007 */ public class RandomItemTypeGui extends HelperGuiBase { public JTextField itemNameField; public JScrollPane scrollPane; public JTextPane jsonPane; public JButton parseFromJsonButton; public JButton saveButton; public JButton updateJsonButton; public JButton testButton; public JPanel panel1; public JTextField HTMLTextField; public RandomItemTypeGui(int rewardID, String name, JsonObject inputData, HashMap<String, String> typeMap) { super(rewardID, name, inputData, typeMap); makeAndOpen(); } @Override public void readJson() { if (data.has(TAG_KEY) && data.getAsJsonObject(TAG_KEY).has(DISPLAY_KEY)) itemNameField.setText(readValue(NAME_KEY, data.getAsJsonObject(TAG_KEY).getAsJsonObject(DISPLAY_KEY))); HTMLTextField.setText(readValue(CUSTOMHTML, data)); jsonPane.setText(GSON.toJson(data)); } @Override public void updateJson() { if (!Strings.isNullOrEmpty(itemNameField.getText())) { if (!data.has(TAG_KEY)) data.add(TAG_KEY, new JsonObject()); if (!data.getAsJsonObject(TAG_KEY).has(DISPLAY_KEY)) data.getAsJsonObject(TAG_KEY).add(DISPLAY_KEY, new JsonObject()); storeValue(NAME_KEY, data.getAsJsonObject(TAG_KEY).getAsJsonObject(DISPLAY_KEY), itemNameField.getText()); } if (!Strings.isNullOrEmpty(HTMLTextField.getText())) storeValue(CUSTOMHTML, data, HTMLTextField.getText()); jsonPane.setText(GSON.toJson(data)); } @Override public void setupListeners() { testButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { updateJson(); TestMessage.sendToServer(name, data); } }); saveButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { updateJson(); Configurator.instance.callback(rewardID, name, data); dialog.dispose(); } }); parseFromJsonButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { data = JSON_PARSER.parse(jsonPane.getText()).getAsJsonObject(); readJson(); jsonPane.setForeground(Color.black); } catch (Exception e1) { jsonPane.setForeground(Color.red); e1.printStackTrace(); } } }); updateJsonButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { updateJson(); } }); } @Override public JPanel getPanel() { return panel1; } { // GUI initializer generated by IntelliJ IDEA GUI Designer // >>> IMPORTANT!! <<< // DO NOT EDIT OR ADD ANY CODE HERE! $$$setupUI$$$(); } /** * Method generated by IntelliJ IDEA GUI Designer * >>> IMPORTANT!! <<< * DO NOT edit this method OR call it in your code! * * @noinspection ALL */ private void $$$setupUI$$$() { panel1 = new JPanel(); panel1.setLayout(new GridBagLayout()); final JPanel panel2 = new JPanel(); panel2.setLayout(new GridBagLayout()); GridBagConstraints gbc; gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.weightx = 1.0; gbc.fill = GridBagConstraints.BOTH; panel1.add(panel2, gbc); final JLabel label1 = new JLabel(); label1.setText("Visual editor:"); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.anchor = GridBagConstraints.WEST; panel2.add(label1, gbc); final JLabel label2 = new JLabel(); label2.setText("Type:"); gbc = new GridBagConstraints(); gbc.gridx = 2; gbc.gridy = 0; gbc.anchor = GridBagConstraints.EAST; panel2.add(label2, gbc); final JLabel label3 = new JLabel(); label3.setText("Item name:"); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 1; gbc.anchor = GridBagConstraints.WEST; panel2.add(label3, gbc); itemNameField = new JTextField(); itemNameField.setColumns(20); itemNameField.setToolTipText("I added this cause of easy :p"); gbc = new GridBagConstraints(); gbc.gridx = 1; gbc.gridy = 1; gbc.weightx = 1.0; gbc.anchor = GridBagConstraints.WEST; gbc.fill = GridBagConstraints.HORIZONTAL; panel2.add(itemNameField, gbc); final JLabel label4 = new JLabel(); label4.setText("INT"); gbc = new GridBagConstraints(); gbc.gridx = 2; gbc.gridy = 1; gbc.anchor = GridBagConstraints.EAST; panel2.add(label4, gbc); final JLabel label5 = new JLabel(); label5.setText("Custom HTML:"); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 2; gbc.anchor = GridBagConstraints.EAST; panel2.add(label5, gbc); HTMLTextField = new JTextField(); gbc = new GridBagConstraints(); gbc.gridx = 1; gbc.gridy = 2; gbc.anchor = GridBagConstraints.WEST; gbc.fill = GridBagConstraints.HORIZONTAL; panel2.add(HTMLTextField, gbc); final JLabel label6 = new JLabel(); label6.setText("STRING"); gbc = new GridBagConstraints(); gbc.gridx = 2; gbc.gridy = 2; gbc.anchor = GridBagConstraints.WEST; panel2.add(label6, gbc); final JPanel panel3 = new JPanel(); panel3.setLayout(new GridBagLayout()); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 1; gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.fill = GridBagConstraints.BOTH; panel1.add(panel3, gbc); final JLabel label7 = new JLabel(); label7.setText("Json (any tag put in there wil override a tags from the random item):"); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.anchor = GridBagConstraints.WEST; panel3.add(label7, gbc); scrollPane = new JScrollPane(); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 1; gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.fill = GridBagConstraints.BOTH; panel3.add(scrollPane, gbc); jsonPane = new JTextPane(); jsonPane.setEnabled(true); jsonPane.setText(""); jsonPane.setToolTipText("Make sure you hit \"Parse from JSON\" after editing this!"); scrollPane.setViewportView(jsonPane); final JPanel panel4 = new JPanel(); panel4.setLayout(new GridBagLayout()); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 2; gbc.weightx = 1.0; gbc.fill = GridBagConstraints.BOTH; panel1.add(panel4, gbc); parseFromJsonButton = new JButton(); parseFromJsonButton.setText("Parse from Json"); parseFromJsonButton.setToolTipText("Push the button!"); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.weightx = 1.0; gbc.fill = GridBagConstraints.HORIZONTAL; panel4.add(parseFromJsonButton, gbc); saveButton = new JButton(); saveButton.setText("Save"); saveButton.setToolTipText("Push the button!"); gbc = new GridBagConstraints(); gbc.gridx = 3; gbc.gridy = 0; gbc.weightx = 1.0; gbc.fill = GridBagConstraints.HORIZONTAL; panel4.add(saveButton, gbc); updateJsonButton = new JButton(); updateJsonButton.setText("Update Json"); updateJsonButton.setToolTipText("Push the button!"); gbc = new GridBagConstraints(); gbc.gridx = 1; gbc.gridy = 0; gbc.weightx = 1.0; gbc.fill = GridBagConstraints.HORIZONTAL; panel4.add(updateJsonButton, gbc); testButton = new JButton(); testButton.setText("Test"); testButton.setToolTipText("Push the button!"); gbc = new GridBagConstraints(); gbc.gridx = 2; gbc.gridy = 0; gbc.weightx = 1.0; gbc.fill = GridBagConstraints.HORIZONTAL; panel4.add(testButton, gbc); label3.setLabelFor(itemNameField); } /** * @noinspection ALL */ public JComponent $$$getRootComponent$$$() { return panel1; } }