1 6 7 package org.jdesktop.demo.adventure; 8 9 import java.awt.Component ; 10 import java.awt.Dimension ; 11 import java.awt.GridBagConstraints ; 12 import java.awt.GridBagLayout ; 13 import java.awt.Insets ; 14 import java.text.NumberFormat ; 15 import java.util.HashMap ; 16 import java.util.Map ; 17 18 import javax.swing.BorderFactory ; 19 import javax.swing.DefaultListCellRenderer ; 20 import javax.swing.ImageIcon ; 21 import javax.swing.JButton ; 22 import javax.swing.JFormattedTextField ; 23 import javax.swing.JFrame ; 24 import javax.swing.JLabel ; 25 import javax.swing.JList ; 26 import javax.swing.JPanel ; 27 import javax.swing.JScrollPane ; 28 import javax.swing.JSplitPane ; 29 import javax.swing.JTextArea ; 30 import javax.swing.JTextField ; 31 import javax.swing.text.DefaultFormatterFactory ; 32 import javax.swing.text.NumberFormatter ; 33 import org.jdesktop.swing.binding.TextBinding; 34 import org.jdesktop.dataset.DataSet; 35 import java.io.File ; 36 import org.jdesktop.swing.binding.ImagePanelBinding; 37 import org.jdesktop.swing.binding.DirectListBinding; 38 import org.jdesktop.dataset.DataRelation; 39 import org.jdesktop.dataset.DataRelationTable; 40 import org.jdesktop.dataset.DataSelector; 41 import org.jdesktop.dataset.DataTable; 42 import org.jdesktop.dataset.adapter.DataModelAdapter; 43 import org.jdesktop.dataset.adapter.SelectionModelAdapter; 44 import org.jdesktop.dataset.adapter.TabularDataModelAdapter; 45 import org.jdesktop.demo.Main; 46 import org.jdesktop.swing.JXImagePanel; 47 import org.jdesktop.swing.JXTable; 48 import org.jdesktop.swing.JXTitledPanel; 49 import org.jdesktop.swing.binding.DirectTableBinding; 50 import org.jdesktop.swing.utils.WindowUtils; 51 52 56 public class MainWindow extends JFrame { 57 60 private static final long serialVersionUID = 3544670668122894901L; 61 62 private DataSet ds; 63 private JTextField nameTF; 64 private JTextField locationTF; 65 private JFormattedTextField priceTF; 66 private JTextArea descriptionTA; 67 private JXImagePanel imagePanel; 68 private JList navigator; 69 private JTextField catNameTF; 70 private JXTable activitiesTBL; 71 72 73 public MainWindow() { 74 createDataSet(); 76 77 initComponents(); 79 80 DataModelAdapter packageModel = new DataModelAdapter(ds.getTable("package").getSelector("current")); 82 new TextBinding(locationTF, packageModel, "location"); 83 new TextBinding(priceTF, packageModel, "price"); 84 new TextBinding(nameTF, packageModel, "name"); 85 new TextBinding(descriptionTA, packageModel, "description"); 86 new ImagePanelBinding(imagePanel, packageModel, "imageuri"); 87 TabularDataModelAdapter navModel = new TabularDataModelAdapter(ds.getTable("package")); 88 SelectionModelAdapter sm = new SelectionModelAdapter(ds.getTable("package").getSelector("current")); 89 new DirectListBinding(navigator, navModel, "name", sm); 90 DataModelAdapter categoryDetailModel = new DataModelAdapter(ds.getTable("categoryDetail").getSelector("current")); 91 new TextBinding(catNameTF, categoryDetailModel, "name"); 92 TabularDataModelAdapter activitiesDetailModel = new TabularDataModelAdapter(ds.getTable("activitiesDetail")); 93 new DirectTableBinding(activitiesTBL, activitiesDetailModel, new String []{"name", "description", "price"}); 94 } 95 96 101 private void initComponents() { 102 setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 103 setTitle("Adventure Builder 2.0"); 104 105 JPanel contentPane = new JPanel (new GridBagLayout ()); 107 setContentPane(contentPane); 108 109 JSplitPane splitPane = new JSplitPane (JSplitPane.HORIZONTAL_SPLIT); 110 splitPane.setBorder(BorderFactory.createEmptyBorder()); 111 contentPane.add(splitPane, new GridBagConstraints (0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets (11, 11, 12, 12), 0, 0)); 112 113 JXTitledPanel navigatorPanel = new JXTitledPanel("Packages"); 114 navigator = new JList (); 115 navigator.setCellRenderer(new DefaultListCellRenderer () { 117 Map <String ,ImageIcon > cachedImages = new HashMap <String ,ImageIcon >(); 118 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 119 JLabel lbl = (JLabel )super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 120 return lbl; 133 } 134 }); 135 JScrollPane navSP = new JScrollPane (navigator); 136 navSP.setBorder(BorderFactory.createEmptyBorder()); 137 navigatorPanel.setContentContainer(navSP); 138 splitPane.setLeftComponent(navigatorPanel); 139 140 JPanel detailsPanel = new JPanel (new GridBagLayout ()); 142 imagePanel = new JXImagePanel(); 143 imagePanel.setBorder(BorderFactory.createEmptyBorder()); 144 detailsPanel.add(imagePanel, new GridBagConstraints (0, 0, 1, 4, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets (12, 12, 5, 5), 0, 0)); 145 146 nameTF = new JTextField (); 147 detailsPanel.add(new JLabel ("Name"), new GridBagConstraints (1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets (12, 0, 0, 0), 0, 0)); 148 detailsPanel.add(nameTF, new GridBagConstraints (2, 0, 1, 1, 1.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets (12, 12, 0, 11), 0, 0)); 149 150 locationTF = new JTextField (); 151 detailsPanel.add(new JLabel ("Location"), new GridBagConstraints (1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets (5, 0, 0, 0), 0, 0)); 152 detailsPanel.add(locationTF, new GridBagConstraints (2, 1, 1, 1, 1.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets (5, 12, 0, 11), 0, 0)); 153 154 priceTF = new JFormattedTextField (); 155 NumberFormat amountDisplayFormat = NumberFormat.getCurrencyInstance(); 156 amountDisplayFormat.setMinimumFractionDigits(2); 157 NumberFormat amountEditFormat = NumberFormat.getNumberInstance(); 158 priceTF.setFormatterFactory(new DefaultFormatterFactory ( 159 new NumberFormatter (amountDisplayFormat), 160 new NumberFormatter (amountDisplayFormat), 161 new NumberFormatter (amountEditFormat))); 162 detailsPanel.add(new JLabel ("Price"), new GridBagConstraints (1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets (5, 0, 0, 0), 0, 0)); 163 detailsPanel.add(priceTF, new GridBagConstraints (2, 2, 1, 1, 1.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets (5, 12, 0, 11), 0, 0)); 164 165 catNameTF = new JTextField (); 166 detailsPanel.add(new JLabel ("Category"), new GridBagConstraints (1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets (5, 0, 0, 0), 0, 0)); 167 detailsPanel.add(catNameTF, new GridBagConstraints (2, 3, 1, 1, 1.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets (5, 12, 0, 11), 0, 0)); 168 169 descriptionTA = new JTextArea (); 170 detailsPanel.add(new JLabel ("Description"), new GridBagConstraints (0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets (5, 12, 0, 0), 0, 0)); 171 detailsPanel.add(new JScrollPane (descriptionTA), new GridBagConstraints (0, 5, 3, 1, 1.0, 1.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets (5, 12, 0, 11), 0, 0)); 172 173 activitiesTBL = new JXTable(); 174 detailsPanel.add(new JLabel ("Activities"), new GridBagConstraints (0, 6, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets (5, 12, 0, 0), 0, 0)); 175 detailsPanel.add(new JScrollPane (activitiesTBL), new GridBagConstraints (0, 7, 3, 1, 1.0, 1.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets (5, 12, 11, 11), 0, 0)); 176 177 JXTitledPanel detailPanel = new JXTitledPanel("Package Details"); 178 detailPanel.setContentContainer(detailsPanel); 179 splitPane.setRightComponent(detailPanel); 180 181 JButton saveButton = new JButton ("Save"); 182 saveButton.setMnemonic('S'); 183 saveButton.addActionListener(new java.awt.event.ActionListener () { 184 public void actionPerformed(java.awt.event.ActionEvent evt) { 185 saveButtonActionPerformed(evt); 186 } 187 }); 188 189 JButton refreshButton = new JButton ("Refresh"); 190 refreshButton.setMnemonic('R'); 191 refreshButton.addActionListener(new java.awt.event.ActionListener () { 192 public void actionPerformed(java.awt.event.ActionEvent evt) { 193 refreshButtonActionPerformed(evt); 194 } 195 }); 196 197 199 setSize(new Dimension (800, 600)); 200 setLocation(WindowUtils.getPointForCentering(this)); 201 } 202 203 private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) { ds.readXml(new File ("/home/rb156199/dataset.xml")); 206 207 } 209 private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) { System.out.println(ds.writeXml()); 212 } 214 private void createDataSet() { 215 ds = DataSet.createFromSchema(getClass().getResourceAsStream("resources/dataset.xsd")); 216 DataTable packageTable = ds.getTable("package"); 217 DataSelector currentPackage = packageTable.createSelector("current"); 218 219 DataRelation packageCategoryRelation = ds.createRelation(); 220 packageCategoryRelation.setName("packageCategory"); 221 packageCategoryRelation.setParentColumn(ds.getTable("package").getColumn("catid")); 222 packageCategoryRelation.setChildColumn(ds.getTable("category").getColumn("catid")); 223 224 DataRelation activitiesRelation = ds.createRelation(); 225 activitiesRelation.setName("activities"); 226 activitiesRelation.setParentColumn(ds.getTable("package").getColumn("packageid")); 227 activitiesRelation.setChildColumn(ds.getTable("activity").getColumn("packageid")); 228 229 DataRelationTable categoryDetail = ds.createRelationTable(); 231 categoryDetail.setName("categoryDetail"); 232 categoryDetail.setRelation(packageCategoryRelation); 233 categoryDetail.setParentSelector(currentPackage); 234 235 DataRelationTable activitiesDetail = ds.createRelationTable(); 236 activitiesDetail.setName("activitiesDetail"); 237 activitiesDetail.setRelation(activitiesRelation); 238 activitiesDetail.setParentSelector(currentPackage); 239 240 ds.readXml(getClass().getResourceAsStream("resources/dataset.xml")); 241 } 242 243 public static void main(String [] args) { 244 Main.main(new String []{"-w", "org.jdesktop.demo.adventure.MainWindow"}); 245 } 246 } 247 | Popular Tags |