Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tab indentation can be set from the UI. #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion src/co/uproot/htabs/demo/HTabsDemoApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
Expand All @@ -47,6 +48,8 @@
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import co.uproot.htabs.custom.tabbedpane.CustomTabbedPane;
import co.uproot.htabs.demo.components.ColoredIcon;
Expand Down Expand Up @@ -170,7 +173,48 @@ public void actionPerformed(final ActionEvent e) {
newTabButtons.add(newSiblingBtn, BorderLayout.CENTER);
newTabButtons.add(newChildBtn, BorderLayout.EAST);

topBar.add(radioPanel, BorderLayout.WEST);
final JPanel tabIndentPanel = new JPanel();
tabIndentPanel.setLayout(new BorderLayout());

final JLabel tabIndentLabel = new JLabel("Tab indent: ");

final JTextField tabIndentField = new JTextField(String.valueOf(tabManager.getTabComponentIndent()), 5);
tabIndentField.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
update();
}

@Override
public void removeUpdate(DocumentEvent e) {
update();
}

@Override
public void changedUpdate(DocumentEvent e) {

}

private void update() {
try {
int indent = Integer.parseInt(tabIndentField.getText());
tabManager.setIndent(indent);
} catch (NumberFormatException e) {
// do nothing
}
}
});

tabIndentPanel.add(tabIndentLabel, BorderLayout.WEST);
tabIndentPanel.add(tabIndentField, BorderLayout.EAST);

final JPanel upperLeftPanel = new JPanel();
upperLeftPanel.setLayout(new BorderLayout());

upperLeftPanel.add(radioPanel, BorderLayout.NORTH);
upperLeftPanel.add(tabIndentPanel, BorderLayout.SOUTH);

topBar.add(upperLeftPanel, BorderLayout.WEST);
topBar.add(newTabButtons, BorderLayout.EAST);


Expand Down
45 changes: 45 additions & 0 deletions src/co/uproot/htabs/tabmanager/TabManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.awt.geom.GeneralPath;
import java.awt.geom.Path2D;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.AbstractButton;
Expand Down Expand Up @@ -172,6 +173,29 @@ private int getTabComponentIndent(final Component tabComponent) {
final int tabLevel = getTabLevel(tab);
return getTabComponentIndent(tabLevel);
}

public void setIndent(int indent){
if (!(tabbedPane.getUI() instanceof CustomMetalTabbedPaneUI)){
setTabIndent(indent);
}
setTabComponentIndent(indent);
redraw();
}

private void redraw() {
tabbedPane.revalidate();
tabbedPane.repaint();
for(final Tab tab: tabs){
tab.removeTabContentPane();
}

// TODO: use Google's Collection library to foreach through the original list in reversed order.
List<Tab> reversedTabs = new ArrayList<>(tabs);
Collections.reverse(reversedTabs);
for(final Tab tab: reversedTabs){
tab.addTabContentPane();
}
}

private static int getTabLevel(final Tab tab) {
return tab == null ? 0 : tab.tabLevel;
Expand Down Expand Up @@ -778,7 +802,28 @@ public void collapseTab() {
}
this.setCollapsed(true);
}

private void addTabContentPane() {
List<Tab> reversedChildren = new ArrayList<>(children);
// TODO: use Google's Collection library to foreach through the original list in reversed order.
Collections.reverse(reversedChildren);
for (final Tab child : reversedChildren) {
child.addTabContentPane();
}
addTab(Tab.this, 0);
}

private void removeTabContentPane() {
tabbedPane.remove(tabContentPane);
for (final Tab child : this.children) {
child.removeTabContentPane();
}
}

}

public int getTabComponentIndent() {
return tabComponentIndent;
}

}