001package com.ganteater.ae.desktop.ui; 002 003import java.awt.BorderLayout; 004import java.awt.Dimension; 005import java.awt.event.WindowAdapter; 006import java.awt.event.WindowEvent; 007import java.util.LinkedList; 008import java.util.Map.Entry; 009import java.util.Properties; 010import java.util.Set; 011 012import javax.swing.DefaultCellEditor; 013import javax.swing.JCheckBox; 014import javax.swing.JDialog; 015import javax.swing.JOptionPane; 016import javax.swing.JScrollPane; 017import javax.swing.JTable; 018import javax.swing.table.AbstractTableModel; 019import javax.swing.table.TableColumn; 020 021import org.apache.commons.lang.StringUtils; 022 023import com.ganteater.ae.AEWorkspace; 024import com.ganteater.ae.desktop.InputDialogType; 025import com.ganteater.ae.desktop.util.UIUtils; 026 027public class CheckpointsDialog extends AbstractTableModel { 028 029 private static final long serialVersionUID = -7042738069522850806L; 030 private static final String CP_DIALOG_PREF_PROP_NAME = "CheckpointDialog"; 031 public static final String REQUIRED_DIALOGS = "Very Important Dialogs"; 032 033 private static CheckpointsDialog instance; 034 private JDialog dialog; 035 036 public JDialog getDialog() { 037 return dialog; 038 } 039 040 private transient LinkedList<CheckpointsRec> checkpoints = new LinkedList<>(); 041 private JTable table; 042 043 private CheckpointsDialog() { 044 } 045 046 public void showDialog() { 047 if (dialog == null) { 048 init(); 049 } 050 051 int size = refresh(); 052 053 int height = size * 19 + 80; 054 UIUtils.applyPreferedView(dialog, CP_DIALOG_PREF_PROP_NAME, height, 300); 055 dialog.setVisible(true); 056 } 057 058 private void init() { 059 dialog = new JDialog(JOptionPane.getRootFrame(), REQUIRED_DIALOGS, false); 060 dialog.setPreferredSize(new Dimension(300, 400)); 061 dialog.addWindowListener(new WindowAdapter() { 062 @Override 063 public void windowClosing(WindowEvent e) { 064 UIUtils.saveDialogPreferedView(dialog, CP_DIALOG_PREF_PROP_NAME); 065 } 066 067 @Override 068 public void windowDeactivated(WindowEvent e) { 069 UIUtils.saveDialogPreferedView(dialog, CP_DIALOG_PREF_PROP_NAME); 070 } 071 }); 072 dialog.setPreferredSize(new Dimension(300, 400)); 073 074 table = new JTable(this); 075 TableColumn column = table.getColumnModel().getColumn(0); 076 column.setMaxWidth(40); 077 column.setHeaderValue("VID"); 078 column.setCellEditor(new DefaultCellEditor(new JCheckBox())); 079 080 TableColumn column1 = table.getColumnModel().getColumn(1); 081 column1.setHeaderValue("Dialog Name"); 082 083 dialog.getContentPane().add(new JScrollPane(table), BorderLayout.CENTER); 084 dialog.pack(); 085 } 086 087 public int getColumnCount() { 088 return 2; 089 } 090 091 public int getRowCount() { 092 return checkpoints.size(); 093 } 094 095 public Object getValueAt(int row, int coll) { 096 CheckpointsRec checkpointsRec = checkpoints.get(row); 097 if (coll == 0) { 098 return checkpointsRec.state; 099 } 100 String name = checkpointsRec.name; 101 InputDialogType type = checkpointsRec.type; 102 if (type == InputDialogType.TASKS) { 103 name = "<html><body><strong>" + name + "</strong></body></html>"; 104 } 105 return name; 106 } 107 108 @Override 109 public Class<?> getColumnClass(int coll) { 110 if (coll == 0) 111 return Boolean.class; 112 return String.class; 113 } 114 115 @Override 116 public boolean isCellEditable(int rowIndex, int columnIndex) { 117 return columnIndex == 0; 118 } 119 120 @Override 121 public void setValueAt(Object arg0, int row, int coll) { 122 CheckpointsRec checkpointsRec = checkpoints.get(row); 123 String name = checkpointsRec.name; 124 if (coll == 0) { 125 checkpointsRec.state = (Boolean) arg0; 126 CheckPointBox.setCheckPointFlag(checkpointsRec.type, name, checkpointsRec.state); 127 } 128 } 129 130 public int refresh() { 131 Properties userPreferences = AEWorkspace.getUserPreferences(); 132 if (userPreferences != null) { 133 Set<Entry<Object, Object>> entrySet = userPreferences.entrySet(); 134 135 int size = checkpoints.size(); 136 137 for (Entry<Object, Object> entry : entrySet) { 138 String recStr = (String) entry.getKey(); 139 String prefix = CheckPointBox.CHECKPOINT_PREFIX; 140 if (recStr.startsWith(prefix)) { 141 String name = StringUtils.substringAfterLast(recStr, "."); 142 if (StringUtils.isNotEmpty(name)) { 143 CheckpointsRec rec = getRec(name); 144 String type = StringUtils.substringBetween(recStr, prefix + ".", "."); 145 rec.type = InputDialogType.valueOf(type); 146 rec.state = Boolean.valueOf((String) entry.getValue()); 147 if (!checkpoints.contains(rec)) { 148 checkpoints.add(rec); 149 } 150 } 151 } 152 } 153 154 if (size == 0) { 155 checkpoints.sort((o1, o2) -> o1.name.compareTo(o2.name)); 156 } 157 } 158 159 if (table != null) { 160 table.invalidate(); 161 table.repaint(); 162 } 163 return checkpoints.size(); 164 } 165 166 private CheckpointsRec getRec(String name) { 167 for (CheckpointsRec checkpointsRec : checkpoints) { 168 if (StringUtils.equals(checkpointsRec.name, name)) { 169 return checkpointsRec; 170 } 171 } 172 return new CheckpointsRec(name); 173 } 174 175 public void updated(String name) { 176 for (CheckpointsRec checkpointsRec : checkpoints) { 177 if (StringUtils.equals(checkpointsRec.name, name)) { 178 checkpoints.remove(checkpointsRec); 179 checkpoints.add(0, checkpointsRec); 180 break; 181 } 182 } 183 refresh(); 184 } 185 186 public static CheckpointsDialog getInstance() { 187 if (instance == null) { 188 instance = new CheckpointsDialog(); 189 } 190 return instance; 191 } 192 193}