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                name = name.replaceAll("<[^>]*>", "");
102                InputDialogType type = checkpointsRec.type;
103                if (type == InputDialogType.TASKS) {
104                        name = "<html><body><strong>" + name + "</strong></body></html>";
105                }
106                return name;
107        }
108
109        @Override
110        public Class<?> getColumnClass(int coll) {
111                if (coll == 0)
112                        return Boolean.class;
113                return String.class;
114        }
115
116        @Override
117        public boolean isCellEditable(int rowIndex, int columnIndex) {
118                return columnIndex == 0;
119        }
120
121        @Override
122        public void setValueAt(Object arg0, int row, int coll) {
123                CheckpointsRec checkpointsRec = checkpoints.get(row);
124                String name = checkpointsRec.name;
125                if (coll == 0) {
126                        checkpointsRec.state = (Boolean) arg0;
127                        CheckPointBox.setCheckPointFlag(checkpointsRec.type, name, checkpointsRec.state);
128                }
129        }
130
131        public int refresh() {
132                Properties userPreferences = AEWorkspace.getUserPreferences();
133                if (userPreferences != null) {
134                        Set<Entry<Object, Object>> entrySet = userPreferences.entrySet();
135
136                        int size = checkpoints.size();
137
138                        for (Entry<Object, Object> entry : entrySet) {
139                                String recStr = (String) entry.getKey();
140                                String prefix = CheckPointBox.CHECKPOINT_PREFIX;
141                                if (recStr.startsWith(prefix)) {
142                                        String name = StringUtils.substringAfterLast(recStr, ".");
143                                        if (StringUtils.isNotEmpty(name)) {
144                                                CheckpointsRec rec = getRec(name);
145                                                String type = StringUtils.substringBetween(recStr, prefix + ".", ".");
146                                                rec.type = InputDialogType.valueOf(type);
147                                                rec.state = Boolean.valueOf((String) entry.getValue());
148                                                if (!checkpoints.contains(rec)) {
149                                                        checkpoints.add(rec);
150                                                }
151                                        }
152                                }
153                        }
154
155                        if (size == 0) {
156                                checkpoints.sort((o1, o2) -> o1.name.compareTo(o2.name));
157                        }
158                }
159
160                if (table != null) {
161                        table.invalidate();
162                        table.repaint();
163                }
164                return checkpoints.size();
165        }
166
167        private CheckpointsRec getRec(String name) {
168                for (CheckpointsRec checkpointsRec : checkpoints) {
169                        if (StringUtils.equals(checkpointsRec.name, name)) {
170                                return checkpointsRec;
171                        }
172                }
173                return new CheckpointsRec(name);
174        }
175
176        public void updated(String name) {
177                for (CheckpointsRec checkpointsRec : checkpoints) {
178                        if (StringUtils.equals(checkpointsRec.name, name)) {
179                                checkpoints.remove(checkpointsRec);
180                                checkpoints.add(0, checkpointsRec);
181                                break;
182                        }
183                }
184                refresh();
185        }
186
187        public static CheckpointsDialog getInstance() {
188                if (instance == null) {
189                        instance = new CheckpointsDialog();
190                }
191                return instance;
192        }
193
194}