Skip to content

Commit

Permalink
inline toolbar into header
Browse files Browse the repository at this point in the history
  • Loading branch information
VISTALL committed Dec 12, 2024
1 parent 6b24a9e commit 9bb02a5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,18 @@
import consulo.project.Project;
import consulo.project.ui.wm.ToolWindowFactory;
import consulo.ui.annotation.RequiredUIAccess;
import consulo.ui.ex.action.ActionGroup;
import consulo.ui.ex.action.ActionManager;
import consulo.ui.ex.action.ActionToolbar;
import consulo.ui.ex.awt.SimpleToolWindowPanel;
import consulo.ui.ex.action.AnAction;
import consulo.ui.ex.content.Content;
import consulo.ui.ex.content.ContentFactory;
import consulo.ui.ex.content.ContentManager;
import consulo.ui.ex.toolWindow.ToolWindow;
import consulo.ui.ex.toolWindow.ToolWindowAnchor;
import consulo.ui.image.Image;

import jakarta.annotation.Nonnull;

import java.util.ArrayList;
import java.util.List;

/**
* @author VISTALL
* @since 2020-06-01
Expand Down Expand Up @@ -70,7 +69,6 @@ public LocalizeValue getDisplayName()
return LocalizeValue.localizeTODO("Database");
}


@Nonnull
@Override
public String getId()
Expand All @@ -86,26 +84,22 @@ public void createToolWindowContent(@Nonnull Project project, @Nonnull ToolWindo

ContentFactory factory = contentManager.getFactory();

SimpleToolWindowPanel toolWindowPanel = new SimpleToolWindowPanel(true, true);

DatabaseTreePanel panel = new DatabaseTreePanel(project);
toolWindowPanel.setContent(panel.getRootPanel());

ActionGroup.Builder builder = ActionGroup.newImmutableBuilder();
builder.add(new AddDataSourceAction());
builder.add(new RemoveDataSourceAction(null));
builder.add(new EditDataSourceAction());
builder.addSeparator();
builder.add(new RefreshDataSourcesAction());
List<AnAction> actions = new ArrayList<>();
actions.add(new AddDataSourceAction());
actions.add(new RemoveDataSourceAction(null));
actions.add(new EditDataSourceAction());
actions.add(new RefreshDataSourcesAction());

ActionToolbar toolbar = ActionManager.getInstance().createActionToolbar("DatabaseToolWindow", builder.build(), true);
toolbar.setTargetComponent(panel.getRootPanel());
toolWindowPanel.setToolbar(toolbar.getComponent());
toolWindow.setTitleActions(actions.toArray(AnAction.EMPTY_ARRAY));

Content content = factory.createContent(toolWindowPanel, null, false);
Content content = factory.createContent(panel.getRootPanel(), null, false);

content.setDisposer(panel);

contentManager.addContent(content);

contentManager.addDataProvider(panel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package consulo.database.impl.toolWindow;

import consulo.component.messagebus.MessageBusConnection;
import consulo.dataContext.DataManager;
import consulo.dataContext.DataProvider;
import consulo.database.datasource.editor.DataSourceEditorManager;
import consulo.database.datasource.jdbc.provider.impl.JdbcTableState;
import consulo.database.datasource.jdbc.ui.tree.DatabaseJdbcTableNode;
Expand All @@ -35,8 +35,10 @@
import consulo.ui.ex.awt.event.DoubleClickListener;
import consulo.ui.ex.awt.tree.*;
import consulo.ui.ex.tree.NodeDescriptor;

import consulo.util.dataholder.Key;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;

import javax.swing.*;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
Expand All @@ -48,11 +50,12 @@
* @author VISTALL
* @since 2020-08-12
*/
public class DatabaseTreePanel implements Disposable
public class DatabaseTreePanel extends JPanel implements Disposable, DataProvider
{
private static final Logger LOG = Logger.getInstance(DatabaseTreePanel.class);

private JPanel myRootPanel;
private final Tree myTree;

public DatabaseTreePanel(@Nonnull Project project)
{
Expand All @@ -61,7 +64,7 @@ public DatabaseTreePanel(@Nonnull Project project)

DatabaseTreeStructure structure = new DatabaseTreeStructure(project);
StructureTreeModel<DatabaseTreeStructure> treeModel = new StructureTreeModel<>(structure, this);
Tree tree = new Tree(new AsyncTreeModel(treeModel, this))
myTree = new Tree(new AsyncTreeModel(treeModel, this))
{
@Override
public final int getToggleClickCount()
Expand All @@ -81,27 +84,27 @@ public final int getToggleClickCount()
}
};

tree.addTreeExpansionListener(new TreeExpansionListener()
myTree.addTreeExpansionListener(new TreeExpansionListener()
{
@Override
public void treeExpanded(TreeExpansionEvent treeExpansionEvent)
{
workspaceManager.setTreeState(TreeState.createOn(tree));
workspaceManager.setTreeState(TreeState.createOn(myTree));
}

@Override
public void treeCollapsed(TreeExpansionEvent treeExpansionEvent)
{
workspaceManager.setTreeState(TreeState.createOn(tree));
workspaceManager.setTreeState(TreeState.createOn(myTree));
}
});
tree.setRootVisible(false);
myTree.setRootVisible(false);
new DoubleClickListener()
{
@Override
protected boolean onDoubleClick(MouseEvent event)
{
TreePath path = TreeUtil.getSelectedPathIfOne(tree);
TreePath path = TreeUtil.getSelectedPathIfOne(myTree);
if(path != null)
{
Object node = TreeUtil.getLastUserObject(path);
Expand All @@ -117,7 +120,7 @@ protected boolean onDoubleClick(MouseEvent event)

return false;
}
}.installOn(tree);
}.installOn(myTree);

MessageBusConnection connection = project.getMessageBus().connect(this);
connection.subscribe(DataSourceListener.class, new DataSourceListener()
Expand All @@ -138,45 +141,47 @@ public void dataUpdated(@Nonnull DataSource dataSource, @Nonnull Object value)
}
});

DataManager.registerDataProvider(myRootPanel, key ->
{
if(key == DataSourceKeys.TREE)
{
return tree;
}
else if(key == DataSourceKeys.DATASOURCE)
{
TreePath path = TreeUtil.getSelectedPathIfOne(tree);
if(path != null)
{
Object lastUserObject = TreeUtil.getLastUserObject(path);
if(lastUserObject instanceof DatabaseSourceNode)
{
return ((DatabaseSourceNode) lastUserObject).getValue();
}
}
}
return null;
});

TreeState treeState = workspaceManager.getTreeState();
if(treeState != null)
{
treeState.applyTo(tree);
treeState.applyTo(myTree);
}
else
{
TreeUtil.expand(tree, 2);
TreeUtil.expand(myTree, 2);
}

myRootPanel.add(ScrollPaneFactory.createScrollPane(tree, true));
myRootPanel.add(ScrollPaneFactory.createScrollPane(myTree, true));
}

public JPanel getRootPanel()
{
return myRootPanel;
}

@Nullable
@Override
public Object getData(@Nonnull Key<?> key)
{
if(key == DataSourceKeys.TREE)
{
return myTree;
}
else if(key == DataSourceKeys.DATASOURCE)
{
TreePath path = TreeUtil.getSelectedPathIfOne(myTree);
if(path != null)
{
Object lastUserObject = TreeUtil.getLastUserObject(path);
if(lastUserObject instanceof DatabaseSourceNode)
{
return ((DatabaseSourceNode) lastUserObject).getValue();
}
}
}
return null;
}

@Override
public void dispose()
{
Expand Down

0 comments on commit 9bb02a5

Please sign in to comment.