| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hivegui.docking; |
| 16 | |
| 17 | import java.awt.Component; |
| 18 | import java.awt.Container; |
| 19 | import java.awt.KeyboardFocusManager; |
| 20 | |
| 21 | import javax.swing.JTabbedPane; |
| 22 | |
| 23 | import org.apache.commons.logging.Log; |
| 24 | import org.apache.commons.logging.LogFactory; |
| 25 | import org.flexdock.view.View; |
| 26 | import org.flexdock.view.Viewport; |
| 27 | |
| 28 | public final class DockingHelper |
| 29 | { |
| 30 | static private final Log _logger = LogFactory.getLog(DockingHelper.class); |
| 31 | |
| 32 | private DockingHelper() |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | static public boolean selectView(Viewport port, String id) |
| 37 | { |
| 38 | // Get current focused component |
| 39 | KeyboardFocusManager focusManager = |
| 40 | KeyboardFocusManager.getCurrentKeyboardFocusManager(); |
| 41 | Component focused = focusManager.getPermanentFocusOwner(); |
| 42 | // Component focused = focusManager.getFocusOwner(); |
| 43 | try |
| 44 | { |
| 45 | Component docked = port.getDockedComponent(); |
| 46 | if (docked instanceof JTabbedPane) |
| 47 | { |
| 48 | JTabbedPane tabs = (JTabbedPane) docked; |
| 49 | for (Component tab: tabs.getComponents()) |
| 50 | { |
| 51 | View view = (View) tab; |
| 52 | if (view.getPersistentId().equals(id)) |
| 53 | { |
| 54 | tabs.setSelectedComponent(view); |
| 55 | return true; |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | return false; |
| 60 | } |
| 61 | finally |
| 62 | { |
| 63 | // Restore previously focused component |
| 64 | if (focused != null) |
| 65 | { |
| 66 | // focused.requestFocusInWindow(); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | static public void trace(Component comp, String indent) |
| 72 | { |
| 73 | _logger.debug(indent + comp.getName() + " [" + comp.getClass() + "]"); |
| 74 | if (comp instanceof Container) |
| 75 | { |
| 76 | for (Component child: ((Container) comp).getComponents()) |
| 77 | { |
| 78 | trace(child, indent + " "); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |