class DetermineCookieFrame(JFrame): """ This is the GUI for for the user to control the actions when determining which cookie is the session cookie. """ def __init__(self, callbacks, selected_message): super(DetermineCookieFrame, self).__init__() self.callbacks = callbacks self.selected_message = selected_message self.windowClosing = self.close def loadPanel(self): panel = JPanel() panel.setLayout(BoxLayout(panel, BoxLayout.Y_AXIS)) bottomButtonBarPanel = JPanel() bottomButtonBarPanel.setLayout( BoxLayout(bottomButtonBarPanel, BoxLayout.X_AXIS)) bottomButtonBarPanel.setAlignmentX(1.0) self.runButton = JButton("Run", actionPerformed=self.start) self.cancelButton = JButton("Close", actionPerformed=self.cancel) bottomButtonBarPanel.add(Box.createHorizontalGlue()) bottomButtonBarPanel.add(self.runButton) bottomButtonBarPanel.add(self.cancelButton) # Dimension(width,height) bottom = JPanel() bottom.setLayout(BoxLayout(bottom, BoxLayout.X_AXIS)) bottom.setAlignmentX(1.0) self.progressBar = JProgressBar() self.progressBar.setIndeterminate(False) self.progressBar.setMaximum(100) self.progressBar.setValue(0) bottom.add(self.progressBar) self.statusTextArea = JTextArea() self.statusTextArea.setEditable(False) scrollPane = JScrollPane(self.statusTextArea) scrollPanel = JPanel() scrollPanel.setLayout(BoxLayout(scrollPanel, BoxLayout.X_AXIS)) scrollPanel.setAlignmentX(1.0) scrollPanel.add(scrollPane) panel.add(scrollPanel) panel.add(bottomButtonBarPanel) panel.add(bottom) self.add(panel) self.setTitle("Determine Session Cookie(s)") self.setSize(450, 300) self.setLocationRelativeTo(None) self.setVisible(True) original_request_bytes = self.selected_message.getRequest() http_service = self.selected_message.getHttpService() helpers = self.callbacks.getHelpers() request_info = helpers.analyzeRequest(http_service, original_request_bytes) parameters = request_info.getParameters() cookie_parameters = [ parameter for parameter in parameters if parameter.getType() == IParameter.PARAM_COOKIE ] num_requests_needed = len(cookie_parameters) + 2 self.statusTextArea.append( "This may require up to " + str(num_requests_needed) + " requests to be made. Hit 'Run' to begin.\n") def start(self, event): global cancelThread cancelThread = False self.runButton.setEnabled(False) self.cancelButton.setText("Cancel") thread = ThreadDetermineCookie(self.callbacks, self.selected_message, self.statusTextArea, self.progressBar) thread.start() def cancel(self, event): self.setVisible(False) self.dispose() def close(self, event): global cancelThread cancelThread = True
class ToolsProgressDialog(JDialog, PropertyChangeListener): def __init__(self, parent, title, modal, app): """Restore the tools data by: - downloading the tools list - downloading the tools jar files - extracting the tools data from jar files """ self.app = app border = BorderFactory.createEmptyBorder(5, 7, 5, 7) mainPane = self.getContentPane() mainPane.setLayout(BorderLayout(5, 5)) mainPane.setBorder(border) self.setPreferredSize(Dimension(400, 200)) self.taskOutput = JMultilineLabel( self.app.strings.getString("checking_for_updates")) self.taskOutput.setMaxWidth(350) self.progressBar = JProgressBar(0, 100, value=0, stringPainted=True) self.progressBar.setIndeterminate(True) self.cancelBtn = JButton("Cancel", actionPerformed=self.on_cancelBtn_clicked, enabled=True) self.okBtn = JButton("Ok", actionPerformed=self.on_okBtn_clicked, enabled=False) mainPane.add(self.taskOutput, BorderLayout.CENTER) bottomPanel = JPanel(BorderLayout(5, 5)) bottomPanel.add(self.progressBar, BorderLayout.PAGE_START) buttonsPanel = JPanel() buttonsPanel.add(self.okBtn) buttonsPanel.add(self.cancelBtn) bottomPanel.add(buttonsPanel, BorderLayout.PAGE_END) mainPane.add(bottomPanel, BorderLayout.PAGE_END) self.pack() self.task = DownloaderTask(self) self.task.addPropertyChangeListener(self) self.task.execute() def propertyChange(self, e): # Invoked when task's progress property changes. if e.propertyName == "progress": if e.newValue == 2: text = self.app.strings.getString("downloading_tools_list") elif e.newValue == 3: text = self.app.strings.getString( "tools_not_downloaded_warning") elif e.newValue == 5: text = self.app.strings.getString("downloading_tools_data") elif e.newValue == 6: text = self.app.strings.getString( "i_cannot_download_the_tool_files") elif e.newValue == 7: text = self.app.strings.getString("extracting_jar_files") elif e.newValue == 8: text = self.app.strings.getString("tools_updated") self.taskOutput.setText("<html>" + text + "</html>") def on_cancelBtn_clicked(self, e): self.dispose() self.task.cancel(True) def on_okBtn_clicked(self, e): self.dispose()
class ToolsProgressDialog(JDialog, PropertyChangeListener): def __init__(self, parent, title, modal, app): """Restore the tools data by: - downloading the tools list - downloading the tools jar files - extracting the tools data from jar files """ self.app = app border = BorderFactory.createEmptyBorder(5, 7, 5, 7) mainPane = self.getContentPane() mainPane.setLayout(BorderLayout(5, 5)) mainPane.setBorder(border) self.setPreferredSize(Dimension(400, 200)) self.taskOutput = JMultilineLabel(self.app.strings.getString("checking_for_updates")) self.taskOutput.setMaxWidth(350) self.progressBar = JProgressBar(0, 100, value=0, stringPainted=True) self.progressBar.setIndeterminate(True) self.cancelBtn = JButton("Cancel", actionPerformed=self.on_cancelBtn_clicked, enabled=True) self.okBtn = JButton("Ok", actionPerformed=self.on_okBtn_clicked, enabled=False) mainPane.add(self.taskOutput, BorderLayout.CENTER) bottomPanel = JPanel(BorderLayout(5, 5)) bottomPanel.add(self.progressBar, BorderLayout.PAGE_START) buttonsPanel = JPanel() buttonsPanel.add(self.okBtn) buttonsPanel.add(self.cancelBtn) bottomPanel.add(buttonsPanel, BorderLayout.PAGE_END) mainPane.add(bottomPanel, BorderLayout.PAGE_END) self.pack() self.task = DownloaderTask(self) self.task.addPropertyChangeListener(self) self.task.execute() def propertyChange(self, e): # Invoked when task's progress property changes. if e.propertyName == "progress": if e.newValue == 2: text = self.app.strings.getString("downloading_tools_list") elif e.newValue == 3: text = self.app.strings.getString("tools_not_downloaded_warning") elif e.newValue == 5: text = self.app.strings.getString("downloading_tools_data") elif e.newValue == 6: text = self.app.strings.getString("i_cannot_download_the_tool_files") elif e.newValue == 7: text = self.app.strings.getString("extracting_jar_files") elif e.newValue == 8: text = self.app.strings.getString("tools_updated") self.taskOutput.setText("<html>" + text + "</html>") def on_cancelBtn_clicked(self, e): self.dispose() self.task.cancel(True) def on_okBtn_clicked(self, e): self.dispose()
class DetermineCookieFrame(JFrame): """ This is the GUI for for the user to control the actions when determining which cookie is the session cookie. """ def __init__(self, callbacks, selected_message): super(DetermineCookieFrame, self).__init__() self.callbacks = callbacks self.selected_message = selected_message self.windowClosing = self.close def loadPanel(self): panel = JPanel() panel.setLayout(BoxLayout(panel, BoxLayout.Y_AXIS)) bottomButtonBarPanel = JPanel() bottomButtonBarPanel.setLayout(BoxLayout(bottomButtonBarPanel, BoxLayout.X_AXIS)) bottomButtonBarPanel.setAlignmentX(1.0) self.runButton = JButton("Run", actionPerformed=self.start) self.cancelButton = JButton("Close", actionPerformed=self.cancel) bottomButtonBarPanel.add(Box.createHorizontalGlue()); bottomButtonBarPanel.add(self.runButton) bottomButtonBarPanel.add(self.cancelButton) # Dimension(width,height) bottom = JPanel() bottom.setLayout(BoxLayout(bottom, BoxLayout.X_AXIS)) bottom.setAlignmentX(1.0) self.progressBar = JProgressBar() self.progressBar.setIndeterminate(False) self.progressBar.setMaximum(100) self.progressBar.setValue(0) bottom.add(self.progressBar) self.statusTextArea = JTextArea() self.statusTextArea.setEditable(False) scrollPane = JScrollPane(self.statusTextArea) scrollPanel = JPanel() scrollPanel.setLayout(BoxLayout(scrollPanel, BoxLayout.X_AXIS)) scrollPanel.setAlignmentX(1.0) scrollPanel.add(scrollPane) panel.add(scrollPanel) panel.add(bottomButtonBarPanel) panel.add(bottom) self.add(panel) self.setTitle("Determine Session Cookie(s)") self.setSize(450, 300) self.setLocationRelativeTo(None) self.setVisible(True) original_request_bytes = self.selected_message.getRequest() http_service = self.selected_message.getHttpService() helpers = self.callbacks.getHelpers() request_info = helpers.analyzeRequest(http_service, original_request_bytes) parameters = request_info.getParameters(); cookie_parameters = [parameter for parameter in parameters if parameter.getType() == IParameter.PARAM_COOKIE] num_requests_needed = len(cookie_parameters) + 2 self.statusTextArea.append("This may require up to " + str(num_requests_needed) + " requests to be made. Hit 'Run' to begin.\n") def start(self, event): global cancelThread cancelThread = False self.runButton.setEnabled(False) self.cancelButton.setText("Cancel") thread = ThreadDetermineCookie(self.callbacks, self.selected_message, self.statusTextArea, self.progressBar) thread.start() def cancel(self, event): self.setVisible(False); self.dispose(); def close(self, event): global cancelThread cancelThread = True
class JythonGui(ItemListener): def __init__(self, instructionsURI=''): self.instructionsURI = instructionsURI self.logger = logging.getLogger('sasi_runner_gui') self.logger.addHandler(logging.StreamHandler()) def log_fn(msg): self.log_msg(msg) self.logger.addHandler(FnLogHandler(log_fn)) self.logger.setLevel(logging.DEBUG) self.selected_input_file = None self.selected_output_file = None self.frame = JFrame( "SASI Runner", defaultCloseOperation = WindowConstants.EXIT_ON_CLOSE, ) self.frame.size = (650, 600,) self.main_panel = JPanel() self.main_panel.layout = BoxLayout(self.main_panel, BoxLayout.Y_AXIS) self.frame.add(self.main_panel) self.top_panel = JPanel(SpringLayout()) self.top_panel.alignmentX = Component.CENTER_ALIGNMENT self.main_panel.add(self.top_panel) self.stageCounter = 1 def getStageLabel(txt): label = JLabel("%s. %s" % (self.stageCounter, txt)) self.stageCounter += 1 return label # Instructions link. self.top_panel.add(getStageLabel("Read the instructions:")) instructionsButton = JButton( ('<HTML><FONT color="#000099">' '<U>open instructions</U></FONT><HTML>'), actionPerformed=self.browseInstructions) instructionsButton.setHorizontalAlignment(SwingConstants.LEFT); instructionsButton.setBorderPainted(False); instructionsButton.setOpaque(False); instructionsButton.setBackground(Color.WHITE); instructionsButton.setToolTipText(self.instructionsURI); self.top_panel.add(instructionsButton) # 'Select input' elements. self.top_panel.add(getStageLabel( "Select a SASI .zip file or data folder:")) self.top_panel.add( JButton("Select input...", actionPerformed=self.openInputChooser)) # 'Select output' elements. self.top_panel.add(getStageLabel("Specify an output file:")) self.top_panel.add( JButton("Specify output...", actionPerformed=self.openOutputChooser)) # 'Set result fields' elements. result_fields = [ {'id': 'gear_id', 'label': 'Gear', 'selected': True, 'enabled': False}, {'id': 'substrate_id', 'label': 'Substrate', 'selected': True}, {'id': 'energy_id', 'label': 'Energy', 'selected': False}, {'id': 'feature_id', 'label': 'Feature', 'selected': False}, {'id': 'feature_category_id', 'label': 'Feature Category', 'selected': False} ] self.selected_result_fields = {} resolutionLabelPanel = JPanel(GridLayout(0,1)) resolutionLabelPanel.add(getStageLabel("Set result resolution:")) resolutionLabelPanel.add( JLabel(("<html><i>This sets the specificity with which<br>" "results will be grouped. Note that enabling<br>" "more fields can *greatly* increase resulting<br>" "output sizes and run times.</i>"))) #self.top_panel.add(getStageLabel("Set result resolution:")) self.top_panel.add(resolutionLabelPanel) checkPanel = JPanel(GridLayout(0, 1)) self.top_panel.add(checkPanel) self.resultFieldCheckBoxes = {} for result_field in result_fields: self.selected_result_fields.setdefault( result_field['id'], result_field['selected']) checkBox = JCheckBox( result_field['label'], result_field['selected']) checkBox.setEnabled(result_field.get('enabled', True)) checkBox.addItemListener(self) checkPanel.add(checkBox) self.resultFieldCheckBoxes[checkBox] = result_field # 'Run' elements. self.top_panel.add(getStageLabel("Run SASI: (this might take a while)")) self.run_button = JButton("Run...", actionPerformed=self.runSASI) self.top_panel.add(self.run_button) SpringUtilities.makeCompactGrid( self.top_panel, self.stageCounter - 1, 2, 6, 6, 6, 6) # Progress bar. self.progressBar = JProgressBar(0, 100) self.main_panel.add(self.progressBar) # Log panel. self.log_panel = JPanel() self.log_panel.alignmentX = Component.CENTER_ALIGNMENT self.log_panel.setBorder(EmptyBorder(10,10,10,10)) self.main_panel.add(self.log_panel) self.log_panel.setLayout(BorderLayout()) self.log = JTextArea() self.log.editable = False self.logScrollPane = JScrollPane(self.log) self.logScrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS) self.logScrollBar = self.logScrollPane.getVerticalScrollBar() self.log_panel.add(self.logScrollPane, BorderLayout.CENTER) # File selectors self.inputChooser = JFileChooser() self.inputChooser.fileSelectionMode = JFileChooser.FILES_AND_DIRECTORIES self.outputChooser = JFileChooser() defaultOutputFile = os.path.join(System.getProperty("user.home"), "sasi_project.zip") self.outputChooser.setSelectedFile(File(defaultOutputFile)); self.outputChooser.fileSelectionMode = JFileChooser.FILES_ONLY self.frame.setLocationRelativeTo(None) self.frame.visible = True def browseInstructions(self, event): """ Open a browser to the instructions page. """ browseURI(self.instructionsURI) def itemStateChanged(self, event): """ Listen for checkbox changes. """ checkBox = event.getItemSelectable() is_selected = (event.getStateChange() == ItemEvent.SELECTED) result_field = self.resultFieldCheckBoxes[checkBox] self.selected_result_fields[result_field['id']] = is_selected def log_msg(self, msg): """ Print message to log and scroll to bottom. """ self.log.append(msg + "\n") self.log.setCaretPosition(self.log.getDocument().getLength()) def openInputChooser(self, event): ret = self.inputChooser.showOpenDialog(self.frame) if ret == JFileChooser.APPROVE_OPTION: self.selected_input_file = self.inputChooser.selectedFile self.log_msg("Selected '%s' as input." % self.selected_input_file.path) def openOutputChooser(self, event): ret = self.outputChooser.showSaveDialog(self.frame) if ret == JFileChooser.APPROVE_OPTION: selectedPath = self.outputChooser.selectedFile.path if not selectedPath.endswith('.zip'): zipPath = selectedPath + '.zip' self.outputChooser.setSelectedFile(File(zipPath)) self.selected_output_file = self.outputChooser.selectedFile self.log_msg( "Selected '%s' as output." % self.selected_output_file.path) def runSASI(self, event): try: self.validateParameters() except Exception as e: self.log_msg("ERROR: '%s'" % e) # Run task in a separate thread, so that log # messages will be shown as task progresses. def run_task(): self.tmp_dir = tempfile.mkdtemp(prefix="sasi_runner.") self.db_file = os.path.join(self.tmp_dir, "sasi_runner.db") self.progressBar.setValue(0) self.progressBar.setIndeterminate(True) def get_connection(): engine = create_engine('h2+zxjdbc:////%s' % self.db_file) con = engine.connect() return con try: # Set result fields. result_fields = [] for field_id, is_selected in self.selected_result_fields.items(): if is_selected: result_fields.append(field_id) task = RunSasiTask( input_path=self.selected_input_file.path, output_file=self.selected_output_file.path, logger=self.logger, get_connection=get_connection, config={ 'result_fields': result_fields, 'run_model': { 'run': { 'batch_size': 'auto', } }, 'output': { 'batch_size': 'auto', }, } ) task.call() except Exception as e: self.logger.exception("Could not complete task") self.progressBar.setIndeterminate(False) self.progressBar.setValue(100) try: shutil.rmtree(self.tmp_dir) except: pass Thread(target=run_task).start() def validateParameters(self): return True
class JythonGui(object): def __init__(self, instructionsURI=""): self.instructionsURI = instructionsURI self.logger = logging.getLogger("sasi_gridder_gui") self.logger.addHandler(logging.StreamHandler()) def log_fn(msg): self.log_msg(msg) self.logger.addHandler(FnLogHandler(log_fn)) self.logger.setLevel(logging.DEBUG) self.selected_input_file = None self.selected_output_file = None self.frame = JFrame("SASI Gridder", defaultCloseOperation=WindowConstants.EXIT_ON_CLOSE) self.frame.size = (650, 600) self.main_panel = JPanel() self.main_panel.layout = BoxLayout(self.main_panel, BoxLayout.Y_AXIS) self.frame.add(self.main_panel) self.top_panel = JPanel(SpringLayout()) self.top_panel.alignmentX = Component.CENTER_ALIGNMENT self.main_panel.add(self.top_panel) self.stageCounter = 1 def getStageLabel(txt): label = JLabel("%s. %s" % (self.stageCounter, txt)) self.stageCounter += 1 return label # Instructions link. self.top_panel.add(getStageLabel("Read the instructions:")) instructionsButton = JButton( ('<HTML><FONT color="#000099">' "<U>open instructions</U></FONT><HTML>"), actionPerformed=self.browseInstructions, ) instructionsButton.setHorizontalAlignment(SwingConstants.LEFT) instructionsButton.setBorderPainted(False) instructionsButton.setOpaque(False) instructionsButton.setBackground(Color.WHITE) instructionsButton.setToolTipText(self.instructionsURI) self.top_panel.add(instructionsButton) # Select input elements. self.top_panel.add(getStageLabel("Select an input data folder:")) self.top_panel.add(JButton("Select input...", actionPerformed=self.openInputChooser)) # Select output elements. self.top_panel.add(getStageLabel("Specify an output file:")) self.top_panel.add(JButton("Specify output...", actionPerformed=self.openOutputChooser)) # Run elements. self.top_panel.add(getStageLabel("Run SASI Gridder: (this might take a hwile")) self.run_button = JButton("Run...", actionPerformed=self.runSASIGridder) self.top_panel.add(self.run_button) SpringUtilities.makeCompactGrid(self.top_panel, self.stageCounter - 1, 2, 6, 6, 6, 6) # Progress bar. self.progressBar = JProgressBar(0, 100) self.main_panel.add(self.progressBar) # Log panel. self.log_panel = JPanel() self.log_panel.alignmentX = Component.CENTER_ALIGNMENT self.log_panel.setBorder(EmptyBorder(10, 10, 10, 10)) self.main_panel.add(self.log_panel) self.log_panel.setLayout(BorderLayout()) self.log = JTextArea() self.log.editable = False self.logScrollPane = JScrollPane(self.log) self.logScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS) self.log_panel.add(self.logScrollPane, BorderLayout.CENTER) # File selectors self.inputChooser = JFileChooser() self.inputChooser.fileSelectionMode = JFileChooser.FILES_AND_DIRECTORIES self.outputChooser = JFileChooser() self.outputChooser.fileSelectionMode = JFileChooser.FILES_ONLY defaultOutputFile = os.path.join(System.getProperty("user.home"), "gridded_efforts.csv") self.outputChooser.setSelectedFile(File(defaultOutputFile)) self.frame.setLocationRelativeTo(None) self.frame.visible = True def browseInstructions(self, event): """ Open a browser to the instructions page. """ browseURI(self.instructionsURI) return def log_msg(self, msg): self.log.append(msg + "\n") self.log.setCaretPosition(self.log.getDocument().getLength()) def openInputChooser(self, event): ret = self.inputChooser.showOpenDialog(self.frame) if ret == JFileChooser.APPROVE_OPTION: self.selected_input_file = self.inputChooser.selectedFile self.log_msg("Selected '%s' as input." % self.selected_input_file.path) def openOutputChooser(self, event): ret = self.outputChooser.showSaveDialog(self.frame) if ret == JFileChooser.APPROVE_OPTION: self.selected_output_file = self.outputChooser.selectedFile self.log_msg("Selected '%s' as output." % self.selected_output_file.path) def runSASIGridder(self, event): try: self.validateParameters() except Exception as e: self.log_msg("ERROR: '%s'" % e) # Run task in a separate thread, so that log # messages will be shown as task progresses. def run_task(): self.progressBar.setValue(0) self.progressBar.setIndeterminate(True) try: input_dir = self.selected_input_file.path output_path = self.selected_output_file.path grid_path = os.path.join(input_dir, "grid", "grid.shp") stat_areas_path = os.path.join(input_dir, "stat_areas", "stat_areas.shp") raw_efforts_path = os.path.join(input_dir, "raw_efforts.csv") gear_mappings_path = os.path.join(input_dir, "gear_mappings.csv") gear_mappings = {} with open(gear_mappings_path, "rb") as f: r = csv.DictReader(f) for mapping in r: gear_mappings[mapping["trip_type"]] = mapping["gear_code"] task = SASIGridderTask( grid_path=grid_path, raw_efforts_path=raw_efforts_path, stat_areas_path=stat_areas_path, output_path=output_path, logger=self.logger, gear_mappings=gear_mappings, effort_limit=None, ) task.call() except Exception as e: self.logger.exception("Could not complete task") self.progressBar.setIndeterminate(False) self.progressBar.setValue(100) Thread(target=run_task).start() def validateParameters(self): return True