@SuppressWarnings("unchecked") // we must be compatible with 1.6 public VersionManagementPanel(final VisualCanvas canvas) { super(canvas); setLayout(new GridLayout()); // TODO @Christian Poliwoda what does manual testing mean? // numbers tested manually Dimension prefScrollPaneDim = new Dimension(100, 30); Dimension visibleRectDim = canvas.getVisibleRect().getSize(); final VersionController controller = canvas.getProjectController().getProject().getProjectFile(); final int numVersions = controller.getNumberOfVersions() - 1; versionData = new Object[numVersions]; ArrayList<RevCommit> versions = new ArrayList<RevCommit>(); try { versions = controller.getVersions(); } catch (IOException ex) { Logger.getLogger(VersionManagementPanel.class.getName()).log(Level.SEVERE, null, ex); } int maxTextwidth = 0; String longestText = null; // the history with timestamp and a short commit message for (int i = 1; i < versions.size(); i++) { String text = // + Message.generateHTMLSpace(3) new Date(versions.get(i).getCommitTime() * 1000L) + ": " + versions.get(i).getShortMessage(); // truncate texts that are too long int maxTextLength = 100; String dots = "..."; int textLength = text.length() - dots.length(); if (textLength > maxTextLength) { text = text.substring(0, maxTextLength) + dots; } versionData[versions.size() - i - 1] = new Version(text, i); if (text.length() > maxTextwidth) { maxTextwidth = text.length(); longestText = text; } } resultModel = new DefaultListModel(); // first init to show all if search not started yet for (int i = 0; i < versionData.length; i++) { resultModel.addElement(versionData[i]); } versionList = new JList(resultModel); // set the width of version managment window // dependent on largest git short message length double maxFontWidth = versionList .getFontMetrics(versionList.getFont()) .getStringBounds(longestText, versionList.getGraphics()) .getWidth(); if (maxFontWidth <= visibleRectDim.width) { prefScrollPaneDim.width = (int) maxFontWidth; } else { if (visibleRectDim.width < 400) { prefScrollPaneDim.width = visibleRectDim.width; } else { prefScrollPaneDim.width = 400; } } versionList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); versionList.setOpaque(false); versionList.setBackground(VSwingUtil.TRANSPARENT_COLOR); versionList.setBorder(new EmptyBorder(3, 3, 3, 3)); Box upperTopBox = Box.createVerticalBox(); // press the commits to top with VerticalGlue // contains search area at top and // search results at the botton Box upperOuterBox = Box.createVerticalBox(); JButton searchButton = new JButton("search"); searchButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { searchAndAddToResultList(); } }); searchField = new JTextArea(); // search area box Box upperBox1 = Box.createHorizontalBox(); upperBox1.add(searchField); upperBox1.add(searchButton); Dimension fieldDim = new Dimension(Short.MAX_VALUE, searchField.getPreferredSize().height); searchField.setMaximumSize(fieldDim); searchField.addKeyListener( new KeyAdapter() { String tmp = ""; @Override public void keyReleased(KeyEvent ke) { searchAndAddToResultList(); } }); // upperOuterBox.add(upperBox1); upperTopBox.add(upperBox1); upperTopBox.add(upperOuterBox); // result area box Box upperBox2 = Box.createHorizontalBox(); upperBox2.add(Box.createHorizontalGlue()); upperBox2.add(versionList); upperBox2.add(Box.createHorizontalGlue()); upperOuterBox.add(upperBox2); upperOuterBox.add(Box.createVerticalGlue()); // added for optical reasons to force correct scrollbar position Box upperInnerBorderPane = Box.createHorizontalBox(); upperInnerBorderPane.add(upperOuterBox); upperInnerBorderPane.setBorder(new EmptyBorder(5, 15, 5, 15)); upperInnerBorderPane.setBackground(VSwingUtil.TRANSPARENT_COLOR); VScrollPane upperScrollPane = new VScrollPane(upperInnerBorderPane); upperScrollPane.setHorizontalScrollBarPolicy(VScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); upperScrollPane.setVerticalScrollBarPolicy(VScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); upperScrollPane.setMinimumSize(prefScrollPaneDim); JSplitPane splitPane = new VSplitPane(JSplitPane.VERTICAL_SPLIT); splitPane.setEnabled(true); // true = transparent splitPane.setBackground(VSwingUtil.TRANSPARENT_COLOR); splitPane.setBorder(new EmptyBorder(5, 5, 5, 5)); splitPane.setDividerLocation(0.5); upperTopBox.add(upperScrollPane); splitPane.add(upperTopBox); // add in the upper part htmlCommit.setBackground(VSwingUtil.TRANSPARENT_COLOR); htmlCommit.setContentType("text/html"); htmlCommit.setOpaque(false); htmlCommit.setEditable(false); htmlCommit.setBorder(new EmptyBorder(0, 15, 0, 15)); Box lowerBox = Box.createVerticalBox(); lowerBox.setAlignmentX(Component.LEFT_ALIGNMENT); lowerBox.add(htmlCommit); lowerBox.add(Box.createVerticalGlue()); VScrollPane lowerScrollPane = new VScrollPane(lowerBox); lowerScrollPane.setHorizontalScrollBarPolicy(VScrollPane.HORIZONTAL_SCROLLBAR_NEVER); lowerScrollPane.setVerticalScrollBarPolicy(VScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); lowerScrollPane.setMinimumSize(new Dimension(0, 0)); // add in the lower part splitPane.setBottomComponent(lowerScrollPane); add(splitPane); versionList.addMouseListener( new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { // show commit message in lower part if clicked on a row // in upper part if (e.getClickCount() == 1) { final VersionController controller = canvas.getProjectController().getProject().getProjectFile(); final int numVersions = controller.getNumberOfVersions() - 1; ArrayList<RevCommit> versions = new ArrayList<RevCommit>(); try { versions = controller.getVersions(); } catch (IOException ex) { Logger.getLogger(VersionManagementPanel.class.getName()) .log(Level.SEVERE, null, ex); } int versionIndex = ((Version) versionList.getSelectedValue()).getVersion(); htmlCommit.setText( "<html>" + "<pre> <font color=white><br>" + "<b>SHA-1:</b> " + versions.get(versionIndex).getName() + "<br><br>" + "<b>Message:</b><br><br>" + versions.get(versionIndex).getFullMessage() + "</pre></p>" + "</html>"); htmlCommit.setCaretPosition(0); } if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e)) { if (((Version) versionList.getSelectedValue()).getVersion() < 2) { VDialog.showMessageDialog( canvas, "Cannot Load Version", "The first version in a project contains no" + " sessions and cannot be loaded!"); return; } // if (VDialog.showConfirmDialog(canvas, // "Checkout Version:", // "<html><div align=Center>" // + "<p>Do you want to checkout the selected" // + " version?<p>" // + "<p><b>Unsaved changes will be lost!</b></p>" // + "</div></html>", // VDialog.DialogType.YES_NO) != VDialog.YES) { // return; // } int answer = VDialog.showConfirmDialog( canvas, "Checkout Version:", "<html><div align=Center>" + "<p>Checking out selected version.<p><br>" + "<p>Do you want to save the current session?</p><br>" + "<p><b>Unsaved changes will be lost!</b></p>" + "</div></html>", new String[] {"Save", "Discard", "Cancel"}); if (answer == 0) { try { canvas.getProjectController().saveProject(false); } catch (IOException ex) { Logger.getLogger(VersionManagement.class.getName()).log(Level.SEVERE, null, ex); VDialog.showMessageDialog( canvas, "Cannot save Project:", "<html><div align=Center>" + "Project cannot be saved!" + "</div></html>"); } } else if (answer == 1) { // nothing to do } else if (answer == 2) { return; } try { int versionIndex = ((Version) versionList.getSelectedValue()).getVersion(); canvas.setActive(false); String currentSessionName = canvas.getProjectController().getCurrentSession(); canvas.getProjectController().close(currentSessionName); controller.checkoutVersion(versionIndex); if (dialog != null) { dialog.close(); dialog = null; } if (canvas .getProjectController() .getProject() .getSessionFileByEntryName(currentSessionName) .exists()) { canvas.getProjectController().open(currentSessionName, false, true); } else { // VDialog.showMessageDialog(canvas, // "Cannot load \"" // + currentSessionName // +"\":", "<html><div align=Center>" // + "<p>The Session " // + Message.EMPHASIZE_BEGIN // + currentSessionName // + Message.EMPHASIZE_END // + " does not exist in the current" // + " version." // + "<p>The <b>Main</b>-Session will" // + "be loaded instead</div></html>"); canvas.getProjectController().open("Main", false, true); } } catch (IOException ex) { Logger.getLogger(VersionManagementPanel.class.getName()) .log(Level.SEVERE, null, ex); } } } }); // setMinimumSize(visibleRectDim); setMaximumSize(visibleRectDim); int width = getPreferredSize().width; setPreferredSize(new Dimension(width, (int) (visibleRectDim.height * 0.5))); } // end constructure
public static void showVersionDialog(final VisualCanvas canvas, Point loc) { // if canvas is inactive don't show dialog if (!canvas.isActive()) { return; } VParamUtil.throwIfNull(canvas); if (dialog != null) { dialog.close(); dialog = null; } moved = false; final VersionManagementPanel componentPanel = new VersionManagementPanel(canvas); dialog = VDialog.showDialogWindow( canvas, "Manage Versions", new VDialogContent(new ResizableContainer(componentPanel)), new String[0]); if (loc != null) { dialog.setLocation(loc); } dialog.addCloseIcon(); dialog.setResizable(false); dialog.setMovable(true); dialog.setActivatable(true); dialog.getStyle().getBaseValues().set(CanvasWindow.FADE_IN_DURATION_KEY, 0.0); dialog.getStyle().getBaseValues().set(CanvasWindow.FADE_OUT_DURATION_KEY, 0.0); dialog.setLayoutController( new VLayoutController() { @Override public void layoutComponent(JComponent c) { try { CanvasWindow w = (CanvasWindow) c; Point loc = w.getLocation(); if (!moved) { Dimension size = w.getSize(); loc.x = (int) (w.getMainCanvas().getVisibleRect().x + w.getMainCanvas().getVisibleRect().getWidth() / 2 - size.width / 2); loc.y = 15 - w.getInsets().top; // - size.height / 2; // check that windows are always inside canvas bounds loc.x = Math.max(loc.x, w.getMainCanvas().getVisibleRect().x); loc.y = Math.max(loc.y, w.getMainCanvas().getVisibleRect().y - w.getInsets().top + 15); loc.x = Math.min( loc.x, w.getMainCanvas().getVisibleRect().x + w.getMainCanvas().getVisibleRect().width - w.getWidth()); loc.y = Math.min( loc.y, w.getMainCanvas().getVisibleRect().y + w.getMainCanvas().getVisibleRect().height - w.getHeight()); } w.setLocation(loc); w.resetWindowLocation(); } catch (Exception ex) { ex.printStackTrace(System.err); } } }); dialog.addActionListener( new CanvasActionListener() { @Override public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals(CanvasWindow.CLOSED_ACTION)) { dialog = null; } if (e.getActionCommand().equals(CanvasWindow.MOVE_ACTION)) { moved = true; } if (e.getActionCommand().equals(CanvasWindow.VISIBLE_ACTION)) { VSwingUtil.invokeLater( new Runnable() { @Override public void run() { componentPanel.requestFocus(); } }); } } }); }