swing/samples/net/sf/xframe/swing/demo JXTable2Applet.java,1.1,1.2

Kurt Riede <[email protected]>
Newsgroups gmane.text.xml.xframe.xsddoc.devel
Message-ID <[email protected]>
Update of /cvsroot/xframe/swing/samples/net/sf/xframe/swing/demo
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3556/samples/net/sf/xframe/swing/demo

Modified Files:
	JXTable2Applet.java 
Log Message:
more demos in table

Index: JXTable2Applet.java
===================================================================
RCS file: /cvsroot/xframe/swing/samples/net/sf/xframe/swing/demo/JXTable2Applet.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** JXTable2Applet.java	3 Aug 2004 17:57:34 -0000	1.1
--- JXTable2Applet.java	30 Aug 2004 07:18:07 -0000	1.2
***************
*** 28,39 ****
--- 28,47 ----
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
+ import java.util.ArrayList;
+ import java.util.Date;
+ import java.util.Iterator;
+ import java.util.List;
  
  import javax.swing.BoxLayout;
+ import javax.swing.DefaultCellEditor;
  import javax.swing.JApplet;
  import javax.swing.JButton;
+ import javax.swing.JCheckBox;
+ import javax.swing.JComboBox;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.RootPaneContainer;
+ import javax.swing.table.AbstractTableModel;
  import javax.swing.table.DefaultTableModel;
  import javax.swing.table.TableColumn;
***************
*** 293,297 ****
      private JXTable createTable1() {
          final String[] columnNames = {"from", "to", "from", "to", "from", "to", "from", "to" };
!         table1Model = new DefaultTableModel(columnNames, TABLE_1_ROWS);
          table1 = new JXTable(table1Model);
          final TableColumnModel columnModel = table1.getColumnModel();
--- 301,307 ----
      private JXTable createTable1() {
          final String[] columnNames = {"from", "to", "from", "to", "from", "to", "from", "to" };
!         Saisons saisons = getSampleSaisons();
!         table1Model = new SaisonsTableModel(saisons);
! //        table1Model = new DefaultTableModel(columnNames, TABLE_1_ROWS);
          table1 = new JXTable(table1Model);
          final TableColumnModel columnModel = table1.getColumnModel();
***************
*** 337,344 ****
--- 347,387 ----
          }
          jxTable.setResizingAllowed(false);
+ 
+         // Create a combo box to show that you can use one in a table.
+         JComboBox comboBox = new JComboBox();
+         comboBox.addItem("Red");
+         comboBox.addItem("Orange");
+         comboBox.addItem("Yellow");
+         comboBox.addItem("Green");
+         comboBox.addItem("Blue");
+         comboBox.addItem("Indigo");
+         comboBox.addItem("Violet");
+         columnModel.getColumn(1).setCellEditor(new DefaultCellEditor(comboBox));
+ 
+         JCheckBox checkBox = new JCheckBox();
+         columnModel.getColumn(2).setCellEditor(new DefaultCellEditor(checkBox));
+ 
          return jxTable;
      }
  
      /**
+      * Create a sample business model.
+      */
+     private Saisons getSampleSaisons() {
+         Saisons saisons = new Saisons();
+         for (int i = 0; i < 4; i++) {
+             Saison saison = new Saison();
+             for (int k = 0; k < 3; k++) {
+                 Duration duration = new Duration();
+                 duration.setFrom(new Date());
+                 duration.setTo(new Date());
+                 saison.addDuration(duration);
+             }
+             saisons.addSaison(saison);
+         }
+         return saisons;
+     }
+ 
+     /**
       * create the third table.
       */
***************
*** 356,358 ****
--- 399,506 ----
          return jxTable;
      }
+ 
+     /////////////////////////////////////////
+     // Some business models for the tables
+     /////////////////////////////////////////
+ 
+     private class Saisons {
+         private List saisons = new ArrayList();
+         public void addSaison(Saison saison) { saisons.add(saison); }
+         public Saison getSaison(int i) {
+             if (saisons.size() <= i) {
+                 for (int k = 0; k < i - saisons.size(); k++) {
+                     saisons.add(new Saison());
+                 }
+             }
+             return (Saison) saisons.get(i);
+         }
+         public int getSaisonCount() { return saisons.size(); }
+         public int getMaxDurations() {
+             int max = 0;
+             for (Iterator i = saisons.iterator(); i.hasNext();) {
+                 max = Math.max(max, ((Saison) i.next()).getDurationCount());
+             }
+             return max;
+         }
+     }
+ 
+     private class Saison {
+         private List durations = new ArrayList();
+         public void addDuration(Duration duration) { durations.add(duration); }
+         public int getDurationCount() { return durations.size(); }
+         public Duration getDuration(int i) {
+             if (durations.size() <= i) {
+                 for (int k = 0; k < i - durations.size(); k++) {
+                     durations.add(new Duration());
+                 }
+             }
+             return (Duration) durations.get(i);
+         }
+     }
+ 
+     private class Duration {
+         private Date from = null;
+         private Date to = null;
+         public Date getFrom() { return from; }
+         public void setFrom(Date c) { this.from = c; }
+         public Date getTo() { return to; }
+         public void setTo(Date c) { this.to = c; }
+     }
+ 
+     /////////////////////////////////////////
+     // Adapters from the business model to the swing tables
+     /////////////////////////////////////////
+ 
+     private class SaisonsTableModel extends AbstractTableModel {
+         private Saisons saisons;
+         public SaisonsTableModel(Saisons s) {
+             saisons = s;
+         }
+         public int getColumnCount() {
+             return saisons.getSaisonCount() * 2;
+         }
+         public int getRowCount() {
+             return saisons.getMaxDurations();
+         }
+         public boolean isCellEditable(int rowIndex, int columnIndex) {
+             return true;
+         }
+         public Class getColumnClass(int columnIndex) {
+             return Date.class;
+         }
+         public Object getValueAt(int rowIndex, int columnIndex) {
+             Saison saison = saisons.getSaison(columnIndex / 2);
+             if (saison != null) {
+                 Duration duration = saison.getDuration(rowIndex);
+                 if (duration != null) {
+                     if (columnIndex % 2 == 0) {
+                         return duration.getFrom();
+                     } else {
+                         return duration.getTo();
+                     }
+                 }
+             }
+             return null;
+         }
+         public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
+             Saison saison = saisons.getSaison(columnIndex / 2);
+             if (saison != null) {
+                 Duration duration = saison.getDuration(rowIndex);
+                 if (duration != null) {
+                     if (columnIndex % 2 == 0) {
+                         duration.setFrom((Date) aValue);
+                     } else {
+                         duration.setTo((Date) aValue);
+                     }
+                 }
+             }
+         }
+         public String getColumnName(int columnIndex) {
+             if (columnIndex % 2 == 0) {
+                 return "from";
+             } else {
+                 return "to";
+             }
+         }
+     }
  }



-------------------------------------------------------
This SF.Net email is sponsored by BEA Weblogic Workshop
FREE Java Enterprise J2EE developer tools!
Get your free copy of BEA WebLogic Workshop 8.1 today.
http://ads.osdn.com/?ad_id=5047&alloc_id=10808&op=click
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.