]> git.xonotic.org Git - xonotic/xonotic.git/blob - misc/tools/NexuizDemoRecorder/main/src/main/java/com/nexuiz/demorecorder/ui/swinggui/ApplyTemplateDialog.java
fix lots of CRLFs
[xonotic/xonotic.git] / misc / tools / NexuizDemoRecorder / main / src / main / java / com / nexuiz / demorecorder / ui / swinggui / ApplyTemplateDialog.java
1 package com.nexuiz.demorecorder.ui.swinggui;
2
3 import java.awt.Frame;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6 import java.awt.event.ItemEvent;
7 import java.awt.event.ItemListener;
8 import java.io.File;
9 import java.util.List;
10
11 import javax.swing.JButton;
12 import javax.swing.JCheckBox;
13 import javax.swing.JDialog;
14 import javax.swing.JLabel;
15 import javax.swing.JOptionPane;
16
17 import net.miginfocom.swing.MigLayout;
18
19 import com.nexuiz.demorecorder.application.DemoRecorderUtils;
20 import com.nexuiz.demorecorder.application.jobs.RecordJob;
21
22 public class ApplyTemplateDialog extends JDialog implements ActionListener, ItemListener {
23
24         private static final long serialVersionUID = 4807155579295688578L;
25         private Frame parentFrame;
26         private RecordJobTemplate template;
27         private List<RecordJob> jobs;
28         
29         private JCheckBox engineCB = new JCheckBox("Engine", true);
30         private JCheckBox engineParametersCB = new JCheckBox("Engine parameters", true);
31         private JCheckBox dpVideoDirCB = new JCheckBox("DPVideo directory", true);
32         private JCheckBox relativeDemoPathCB = new JCheckBox("Relative demo path", true);
33         private JCheckBox jobNameCB = new JCheckBox("Job name", true);
34         private JCheckBox demoDirectoryCB = new JCheckBox("Demo directory", true);
35         private JCheckBox execBeforeCapCB = new JCheckBox("Exec before capture", true);
36         private JCheckBox execAfterCB = new JCheckBox("Exec after capture", true);
37         private JCheckBox videoDestination = new JCheckBox("Video destination", true);
38         private JCheckBox pluginSettingsCB = new JCheckBox("Plug-in settings", true);
39         private JCheckBox selectAllCB = new JCheckBox("Select/deselect all", true);
40         
41         private JButton applyButton = new JButton("Apply");
42         private JButton cancelButton = new JButton("Cancel");
43         
44         public ApplyTemplateDialog(Frame owner, RecordJobTemplate template, List<RecordJob> jobs) {
45                 super(owner, true);
46                 this.parentFrame = owner;
47                 this.template = template;
48                 this.jobs = jobs;
49                 
50                 setDefaultCloseOperation(DISPOSE_ON_CLOSE);
51                 setTitle("Apply template");
52                 this.setupLayout();
53         }
54         
55         public void showDialog() {
56                 this.pack();
57                 this.setLocationRelativeTo(this.parentFrame);
58                 this.setVisible(true);
59         }
60
61         private void setupLayout() {
62                 setLayout(new MigLayout());
63                 getContentPane().add(new JLabel("Select which properties you want to apply to the selected jobs"), "wrap");
64                 
65                 this.setupCheckBoxes();
66                 
67                 applyButton.addActionListener(this);
68                 cancelButton.addActionListener(this);
69                 getContentPane().add(applyButton);
70                 getContentPane().add(cancelButton);
71         }
72         
73         private void setupCheckBoxes() {
74                 getContentPane().add(engineCB, "wrap");
75                 getContentPane().add(engineParametersCB, "wrap");
76                 getContentPane().add(dpVideoDirCB, "wrap");
77                 getContentPane().add(relativeDemoPathCB, "wrap");
78                 getContentPane().add(jobNameCB, "wrap");
79                 getContentPane().add(demoDirectoryCB, "wrap");
80                 getContentPane().add(execBeforeCapCB, "wrap");
81                 getContentPane().add(execAfterCB, "wrap");
82                 getContentPane().add(videoDestination, "wrap");
83                 getContentPane().add(pluginSettingsCB, "wrap");
84                 getContentPane().add(selectAllCB, "wrap");
85                 
86                 selectAllCB.addItemListener(this);
87         }
88
89         @Override
90         public void actionPerformed(ActionEvent e) {
91                 if (e.getSource() == applyButton) {
92                         this.applyTemplates();
93                         dispose();
94                 } else if (e.getSource() == cancelButton) {
95                         dispose();
96                 }
97         }
98         
99         private void applyTemplates() {
100                 String errors = "";
101                 for (RecordJob job : this.jobs) {
102                         try {
103                                 this.applyTemplate(job);
104                         } catch (Throwable e) {
105                                 errors += "Job <B>" + job.getJobName() + "</B>: " + e.getMessage() + "<BR>";
106                         }
107                 }
108                 
109                 if (!errors.equals("")) {
110                         //error occurred!
111                         String errorMsg = "<HTML><BODY>Error occurred while trying to apply templates:<BR><BR>" + errors + "</BODY></HTML>";
112                         JOptionPane.showMessageDialog(this.parentFrame, errorMsg, "Error(s) while applying template", JOptionPane.INFORMATION_MESSAGE);
113                 }
114         }
115         
116         private void applyTemplate(RecordJob job) {
117                 if (engineCB.isSelected()) {
118                         job.setEnginePath(template.getEnginePath());
119                 }
120                 if (engineParametersCB.isSelected()) {
121                         job.setEngineParameters(template.getEngineParameters());
122                 }
123                 if (dpVideoDirCB.isSelected()) {
124                         job.setDpVideoPath(template.getDpVideoPath());
125                 }
126                 if (relativeDemoPathCB.isSelected()) {
127                         job.setRelativeDemoPath(template.getRelativeDemoPath());
128                 }
129                 if (jobNameCB.isSelected()) {
130                         job.setJobName(template.getJobName());
131                 }
132                 if (demoDirectoryCB.isSelected()) {
133                         File demoDir = template.getDemoFile();
134                         String demoFileName = DemoRecorderUtils.getJustFileNameOfPath(job.getDemoFile());
135                         String newDemoPath = demoDir.getAbsolutePath() + File.separator + demoFileName;
136                         job.setDemoFile(new File(newDemoPath));
137                 }
138                 if (execBeforeCapCB.isEnabled()) {
139                         job.setExecuteBeforeCap(template.getExecuteBeforeCap());
140                 }
141                 if (execAfterCB.isSelected()) {
142                         job.setExecuteAfterCap(template.getExecuteAfterCap());
143                 }
144                 if (videoDestination.isSelected()) {
145                         File videoDestinatinDir = template.getVideoDestination();
146                         String videoFileName = DemoRecorderUtils.getJustFileNameOfPath(job.getVideoDestination());
147                         String newVideoPath = videoDestinatinDir.getAbsolutePath() + File.separator + videoFileName;
148                         job.setVideoDestination(new File(newVideoPath));
149                 }
150                 if (pluginSettingsCB.isSelected()) {
151                         job.setEncoderPluginSettings(template.getEncoderPluginSettings());
152                 }
153         }
154
155         @Override
156         public void itemStateChanged(ItemEvent e) {
157                 if (e.getSource() == selectAllCB) {
158                         boolean selected = false;
159                         if (e.getStateChange() == ItemEvent.SELECTED) {
160                                 selected = true;
161                         }
162                         
163                         engineCB.setSelected(selected);
164                         engineParametersCB.setSelected(selected);
165                         dpVideoDirCB.setSelected(selected);
166                         relativeDemoPathCB.setSelected(selected);
167                         jobNameCB.setSelected(selected);
168                         demoDirectoryCB.setSelected(selected);
169                         execBeforeCapCB.setSelected(selected);
170                         execAfterCB.setSelected(selected);
171                         videoDestination.setSelected(selected);
172                         pluginSettingsCB.setSelected(selected);
173                 }
174         }
175 }