Java tutorial
package com.mitrais.mwom.gui; import java.awt.BorderLayout; import java.awt.Color; import java.util.Date; import javax.swing.BorderFactory; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.JToolBar; import javax.swing.ListSelectionModel; import javax.swing.border.TitledBorder; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import javax.swing.table.TableColumn; import org.jdesktop.application.Action; import org.jdesktop.application.Application; import org.jdesktop.application.ApplicationActionMap; import org.jdesktop.application.SingleFrameApplication; import com.imagine.component.calendar.CalendarComboBox; import com.jgoodies.forms.layout.CellConstraints; import com.jgoodies.forms.layout.FormLayout; import com.mitrais.mwom.general.LocationXMLProcessor; import com.mitrais.mwom.general.SettingXMLProcessor; /** * This code was edited or generated using CloudGarden's Jigloo * SWT/Swing GUI Builder, which is free for non-commercial * use. If Jigloo is being used commercially (ie, by a corporation, * company or business for any purpose whatever) then you * should purchase a license for each developer using Jigloo. * Please visit www.cloudgarden.com for details. * Use of Jigloo implies acceptance of these licensing terms. * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE. */ public class TimesheetGUI extends SingleFrameApplication { private JPanel topPanel; private JPanel jPanel1; private JButton jButton2; private JButton jButton1; private JButton jButton5; private JButton jButton4; private JButton jButton3; private JLabel jLabel8; private JLabel jLabel7; private JScrollPane jScrollPane2; private JTable jTable2; private JComboBox jComboBox2; private JScrollPane jScrollPane1; private JTable jTable1; private JPanel jPanel2; private JTextField jTextField2; private JLabel jLabel10; private JToolBar jToolBar1; private JPanel jPanel7; private JPanel jPanel4; private JPanel jPanel3; private JLabel jLabel5; private JLabel jLabel6; private JPanel jPanel6; private JToolBar jToolBar2; private JLabel jLabel2; private CalendarComboBox calendarComboBox2; private CalendarComboBox calendarComboBox1; private JComboBox jComboBox1; private JLabel jLabel3; private JTextField jTextField1; private JLabel jLabel1; private static LocationXMLProcessor lxmlp; private static SettingXMLProcessor sxmlp; @Override protected void startup() { { getMainFrame().setSize(1024, 700); } topPanel = new JPanel(); BoxLayout mainFrameLayout = new BoxLayout(getMainFrame().getContentPane(), javax.swing.BoxLayout.X_AXIS); getMainFrame().getContentPane().setLayout(mainFrameLayout); getMainFrame().getContentPane().add(topPanel); BorderLayout topPanelLayout = new BorderLayout(); topPanel.setLayout(topPanelLayout); topPanel.setPreferredSize(new java.awt.Dimension(0, 0)); topPanel.setSize(1024, 700); { jPanel1 = new JPanel(); FormLayout jPanel1Layout = new FormLayout("max(p;1dlu):grow, max(p;1dlu):grow", "fill:max(p;5dlu):grow"); jPanel1.setLayout(jPanel1Layout); topPanel.add(jPanel1, BorderLayout.NORTH); topPanel.add(getJPanel2(), BorderLayout.CENTER); jPanel1.setPreferredSize(new java.awt.Dimension(1016, 224)); { jPanel3 = new JPanel(); BorderLayout jPanel3Layout = new BorderLayout(); jPanel1.add(jPanel3, new CellConstraints("2, 1, 1, 1, fill, default")); jPanel3.setLayout(jPanel3Layout); jPanel3.setBorder(BorderFactory.createTitledBorder("Search")); jPanel3.setSize(300, 230); { jToolBar1 = new JToolBar(); jPanel3.add(jToolBar1, BorderLayout.NORTH); jToolBar1.setPreferredSize(new java.awt.Dimension(315, 32)); jToolBar1.add(getJButton3()); } { jPanel7 = new JPanel(); jPanel3.add(jPanel7, BorderLayout.CENTER); jPanel7.setLayout(null); jPanel7.setPreferredSize(new java.awt.Dimension(372, 152)); jPanel7.setSize(300, 152); jPanel7.add(getJTextField1(), new CellConstraints("2, 4, 3, 1, default, default")); jPanel7.add(getJLabel3(), new CellConstraints("1, 2, 1, 1, default, bottom")); jPanel7.add(getJComboBox1(), new CellConstraints("2, 2, 3, 1, default, default")); } { jLabel1 = new JLabel(); jPanel7.add(jLabel1, new CellConstraints("1, 3, 1, 1, default, bottom")); jPanel7.add(getCalendarComboBox1(), new CellConstraints("2, 3, 1, 1, default, default")); jPanel7.add(getCalendarComboBox2(), new CellConstraints("4, 3, 1, 1, default, default")); jPanel7.add(getJLabel2(), new CellConstraints("3, 3, 1, 1, default, default")); jPanel7.add(getJLabel5(), new CellConstraints("1, 4, 1, 1, default, bottom")); jPanel7.add(getJButton5()); jLabel1.setName("jLabel1"); jLabel1.setBounds(-1, 61, 77, 16); } } { jPanel4 = new JPanel(); BorderLayout jPanel4Layout = new BorderLayout(); jPanel1.add(jPanel4, new CellConstraints("1, 1, 1, 1, fill, default")); jPanel4.setLayout(jPanel4Layout); jPanel4.setBorder(BorderFactory.createTitledBorder(BorderFactory.createTitledBorder(""), "Detail", TitledBorder.LEADING, TitledBorder.ABOVE_TOP)); jPanel4.setName("jPanel4"); jPanel4.add(getJPanel6(), BorderLayout.CENTER); jPanel4.add(getJToolBar2(), BorderLayout.NORTH); } } show(topPanel); } public static void main(String[] args) throws Exception { sxmlp = new SettingXMLProcessor(args[0]); lxmlp = new LocationXMLProcessor(args[1]); launch(TimesheetGUI.class, args); } /** * Returns the action map used by this application. * Actions defined using the Action annotation * are returned by this method */ private ApplicationActionMap getAppActionMap() { return Application.getInstance().getContext().getActionMap(this); } private JTextField getJTextField1() { if (jTextField1 == null) { jTextField1 = new JTextField(); jTextField1.setName("jTextField1"); jTextField1.setBounds(77, 89, 202, 28); } return jTextField1; } private JLabel getJLabel3() { if (jLabel3 == null) { jLabel3 = new JLabel(); jLabel3.setName("jLabel3"); jLabel3.setBounds(0, 16, 77, 16); } return jLabel3; } private JComboBox getJComboBox1() { if (jComboBox1 == null) { jComboBox1 = new JComboBox(); jComboBox1.setModel(new TsCBEmpModel(lxmlp.xmlteamlist, "", "0000002556")); jComboBox1.setRenderer(new TsCBEmpRender(lxmlp.xmlteamlist)); jComboBox1.setBounds(77, 8, 202, 24); } return jComboBox1; } private CalendarComboBox getCalendarComboBox1() { if (calendarComboBox1 == null) { calendarComboBox1 = new CalendarComboBox(); calendarComboBox1.setName("calendarComboBox1"); calendarComboBox1.setDate(UtilDate.getInitWeekDate(new Date())); calendarComboBox1.setBounds(76, 49, 96, 28); } return calendarComboBox1; } private CalendarComboBox getCalendarComboBox2() { if (calendarComboBox2 == null) { calendarComboBox2 = new CalendarComboBox(); calendarComboBox2.setName("calendarComboBox2"); calendarComboBox2.setDate(UtilDate.getEndWeekDate(new Date())); calendarComboBox2.setBounds(182, 49, 96, 28); } return calendarComboBox2; } private JLabel getJLabel2() { if (jLabel2 == null) { jLabel2 = new JLabel(); jLabel2.setName("jLabel2"); jLabel2.setBounds(173, 55, 10, 16); } return jLabel2; } private JLabel getJLabel5() { if (jLabel5 == null) { jLabel5 = new JLabel(); jLabel5.setName("jLabel5"); jLabel5.setBounds(0, 101, 77, 16); } return jLabel5; } private JToolBar getJToolBar2() { if (jToolBar2 == null) { jToolBar2 = new JToolBar(); jToolBar2.setPreferredSize(new java.awt.Dimension(554, 30)); jToolBar2.add(getJButton1()); jToolBar2.add(getJButton2()); } return jToolBar2; } private JPanel getJPanel6() { if (jPanel6 == null) { jPanel6 = new JPanel(); jPanel6.setLayout(null); jPanel6.setPreferredSize(new java.awt.Dimension(475, 146)); jPanel6.setName("jPanel6"); jPanel6.add(getJTextField2()); jPanel6.add(getJComboBox2()); jPanel6.add(getJScrollPane2()); jPanel6.add(getJLabel7()); jPanel6.add(getJLabel10()); jPanel6.add(getJLabel6()); jPanel6.add(getJLabel8()); jPanel6.add(getJButton4()); } return jPanel6; } private JLabel getJLabel6() { if (jLabel6 == null) { jLabel6 = new JLabel(); jLabel6.setName("jLabel6"); jLabel6.setBounds(0, 15, 77, 16); } return jLabel6; } private JLabel getJLabel10() { if (jLabel10 == null) { jLabel10 = new JLabel(); jLabel10.setName("jLabel10"); jLabel10.setBounds(0, 51, 77, 16); } return jLabel10; } private JTextField getJTextField2() { if (jTextField2 == null) { jTextField2 = new JTextField(); jTextField2.setName("jTextField2"); jTextField2.setBounds(83, 40, 272, 28); } return jTextField2; } private JPanel getJPanel2() { if (jPanel2 == null) { jPanel2 = new JPanel(); BorderLayout jPanel2Layout = new BorderLayout(); jPanel2.setLayout(jPanel2Layout); jPanel2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createTitledBorder(""), "Summary", TitledBorder.LEADING, TitledBorder.ABOVE_TOP)); jPanel2.add(getJScrollPane1(), BorderLayout.CENTER); } return jPanel2; } private JTable getJTable1() { if (jTable1 == null) { TsTab1Model jTable1Model = new TsTab1Model(); jTable1 = new JTable(); jTable1.setModel(jTable1Model); jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); jTable1.setAutoCreateRowSorter(true); jTable1.setName("jTable1"); Table1SelectionEvent(); } return jTable1; } private JScrollPane getJScrollPane1() { if (jScrollPane1 == null) { jScrollPane1 = new JScrollPane(); jScrollPane1.setPreferredSize(new java.awt.Dimension(988, 403)); jScrollPane1.setName("jScrollPane1"); jScrollPane1.setViewportView(getJTable1()); } return jScrollPane1; } private JComboBox getJComboBox2() { if (jComboBox2 == null) { jComboBox2 = new JComboBox(); jComboBox2.setModel(new TsCBEmpModel(lxmlp.xmlteamlist, null, null)); jComboBox2.setRenderer(new TsCBEmpRender(lxmlp.xmlteamlist)); jComboBox2.setBounds(83, 8, 205, 24); } return jComboBox2; } private JTable getJTable2() { if (jTable2 == null) { TsTab2Model jTable2Model = new TsTab2Model(); jTable2 = new JTable(); jTable2.setModel(jTable2Model); jTable2.setRowSelectionAllowed(false); jTable2.setDefaultRenderer(Object.class, new TsTab2Render()); jTable2.getTableHeader().setReorderingAllowed(false); } return jTable2; } private JScrollPane getJScrollPane2() { if (jScrollPane2 == null) { jScrollPane2 = new JScrollPane(); jScrollPane2.setBounds(83, 74, 484, 47); jScrollPane2.setViewportView(getJTable2()); } return jScrollPane2; } public void showErrorMesg(String msg) { JOptionPane.showMessageDialog(getMainFrame(), msg, "Error", JOptionPane.ERROR_MESSAGE); } private JLabel getJLabel7() { if (jLabel7 == null) { jLabel7 = new JLabel(); jLabel7.setName("jLabel7"); jLabel7.setBounds(0, 105, 41, 16); } return jLabel7; } @Action public void LoadDetail() { if (getJComboBox2().getSelectedItem().toString().trim().equalsIgnoreCase("")) { showErrorMesg("Employee Id is Mandatory"); return; } TsTab2Model tab2Model = new TsTab2Model(lxmlp.xmlTS, getJComboBox2().getSelectedItem().toString().trim(), getJTextField2().getText().trim()); getJTable2().setModel(tab2Model); getJScrollPane2().setViewportView(getJTable2()); TsTab1Model tab1Model = new TsTab1Model(lxmlp.xmlTS, getJComboBox2().getSelectedItem().toString().trim()); getJTable1().setModel(tab1Model); checkEffort(); getJScrollPane1().setViewportView(getJTable1()); getJTable1().getTableHeader().setEnabled(true); } private JButton getJButton1() { if (jButton1 == null) { jButton1 = new JButton(); jButton1.setBounds(377, 40, 56, 28); jButton1.setName("jButton1"); jButton1.setAction(getAppActionMap().get("LoadDetail")); } return jButton1; } @Action public void SubmitDetail() { if (getJComboBox2().getSelectedItem().toString().trim().equalsIgnoreCase("")) { showErrorMesg("Employee Id is Mandatory"); return; } if (getJTextField2().getText().trim().equalsIgnoreCase("")) { showErrorMesg("Work Order is Mandatory"); return; } //TO DO LOCK TIMESHEET try { XMLHandlerTimesheet Timesheet = new XMLHandlerTimesheet(lxmlp.xmlTS); Timesheet.fileTOCollection(); for (int i = 0; i < getJTable2().getColumnCount(); i++) { Double val = (Double) getJTable2().getValueAt(0, i); String tanggal = UtilDate.strDateToStrDate(getJTable2().getColumnName(i), "dd-MM-yy", "yyMMdd"); if ((val / 1.0) == (new Double(0) / 1.0)) { Timesheet.removeFromNode(getJComboBox2().getSelectedItem().toString().trim(), tanggal, getJTextField2().getText().trim()); } else { Timesheet.setOrAddNode(getJComboBox2().getSelectedItem().toString().trim(), tanggal, getJTextField2().getText().trim(), val.toString()); } } Timesheet.writeToXMLFile(); //refresh tab 1 TsTab1Model tab1Model = new TsTab1Model(lxmlp.xmlTS, getJComboBox2().getSelectedItem().toString().trim()); getJTable1().setModel(tab1Model); checkEffort(); getJScrollPane1().setViewportView(getJTable1()); } catch (Exception e) { showErrorMesg("Error Processing Timesheet " + e.toString()); } //UNLOCK TIMESHEET } private JButton getJButton2() { if (jButton2 == null) { jButton2 = new JButton(); jButton2.setName("jButton2"); jButton2.setAction(getAppActionMap().get("SubmitDetail")); } return jButton2; } private JLabel getJLabel8() { if (jLabel8 == null) { jLabel8 = new JLabel(); jLabel8.setBounds(83, 124, 397, 16); jLabel8.setName("jLabel8"); } return jLabel8; } private JButton getJButton3() { if (jButton3 == null) { jButton3 = new JButton(); jButton3.setName("jButton3"); jButton3.setAction(getAppActionMap().get("LoadSearch")); } return jButton3; } @Action public void LoadSearch() { //TODO Date dt1 = getCalendarComboBox1().getDate(); Date dt2 = getCalendarComboBox2().getDate(); if (dt1.after(dt2) && !dt1.equals(dt2)) { showErrorMesg("Initial Period Must Prior End Period "); return; } TsTab1Model tab1Model = new TsTab1Model(lxmlp.xmlTS, getJComboBox1().getSelectedItem().toString().trim(), dt1, dt2, getJTextField1().getText().trim()); getJTable1().setModel(tab1Model); checkEffort(); getJScrollPane1().setViewportView(getJTable1()); } private void Table1SelectionEvent() { ListSelectionModel rowSM = jTable1.getSelectionModel(); ListSelectionModel colSM = jTable1.getColumnModel().getSelectionModel(); colSM.addListSelectionListener(new ListSelectionListener() { //@Override public void valueChanged(ListSelectionEvent e) { //Ignore extra messages. Tab1OnSelect(e); } }); rowSM.addListSelectionListener(new ListSelectionListener() { @SuppressWarnings("deprecation") public void valueChanged(ListSelectionEvent e) { //Ignore extra messages. Tab1OnSelect(e); } }); } private void Tab1OnSelect(ListSelectionEvent e) { if (e.getValueIsAdjusting()) return; ListSelectionModel lsm = (ListSelectionModel) e.getSource(); if (lsm.isSelectionEmpty()) { System.out.println("No rows are selected."); return; } int selectedRow = jTable1.getSelectedRow(); //determine which coloumn employee Id is String empId = (String) util.GUIUtil.getCellValue(jTable1, selectedRow, "Employee Id"); if (empId != null) { getJComboBox2().setSelectedItem(empId); } String wo = (String) util.GUIUtil.getCellValue(jTable1, selectedRow, "Work Order"); if (wo != null) { getJTextField2().setText(wo); } if (getJComboBox2().getSelectedItem().toString().trim().equalsIgnoreCase("")) { return; } TsTab2Model tab2Model = new TsTab2Model(lxmlp.xmlTS, getJComboBox2().getSelectedItem().toString().trim(), getJTextField2().getText().trim()); getJTable2().setModel(tab2Model); getJScrollPane2().setViewportView(getJTable2()); } private JButton getJButton4() { if (jButton4 == null) { jButton4 = new JButton(); jButton4.setBounds(361, 41, 37, 27); jButton4.setName("jButton4"); jButton4.setAction(getAppActionMap().get("BrowserDetail")); jButton4.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); } return jButton4; } @Action public void BrowserDetail() { GUIUtil.openInBrowser(getJTextField2().getText(), sxmlp.url); } private JButton getJButton5() { if (jButton5 == null) { jButton5 = new JButton(); jButton5.setAction(getAppActionMap().get("BrowserSearch")); jButton5.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); jButton5.setName("jButton5"); jButton5.setBounds(285, 89, 36, 27); } return jButton5; } @Action public void BrowserSearch() { GUIUtil.openInBrowser(getJTextField1().getText(), sxmlp.url); } private void checkEffort() { double sumHours = 0; TableColumn tc; for (int col = 2; col < getJTable1().getColumnCount(); col++) { for (int row = 0; row < getJTable1().getRowCount(); row++) { if (getJTable1().getValueAt(row, col).toString() != "") sumHours += Double.parseDouble(getJTable1().getValueAt(row, col).toString()); } tc = getJTable1().getColumnModel().getColumn(col); if (sumHours > 8) { tc.setCellRenderer(new TsTab1Render(Color.RED, Color.BLACK)); } else if (sumHours < 8) { tc.setCellRenderer(new TsTab1Render(Color.YELLOW, Color.BLACK)); } sumHours = 0; } } }