1 19 20 package org.netbeans.swing.tabcontrol.plaf; 21 22 import org.netbeans.swing.tabcontrol.TabDataModel; 23 24 import javax.swing.*; 25 import java.awt.*; 26 27 33 final class ViewTabLayoutModel implements TabLayoutModel { 34 35 private TabDataModel model; 36 37 private JComponent renderTarget; 38 39 42 public ViewTabLayoutModel(TabDataModel model, 43 JComponent renderTarget) { 44 this.model = model; 45 this.renderTarget = renderTarget; 46 } 47 48 public int getH(int index) { 49 checkIndex(index); 50 Insets insets = renderTarget.getInsets(); 51 return renderTarget.getHeight() - (insets.bottom + insets.top); 52 } 53 54 public int getW(int index) { 55 checkIndex(index); 56 int x = computeX(index); 57 int nextX; 58 if (index < model.size() - 1) { 59 nextX = computeX(index + 1); 60 } else { 61 Insets insets = renderTarget.getInsets(); 63 nextX = renderTarget.getWidth() - insets.right; 64 } 65 return nextX - x; 67 } 68 69 public int getX(int index) { 70 checkIndex(index); 71 return computeX(index); 72 } 73 74 public int getY(int index) { 75 checkIndex(index); 76 return renderTarget.getInsets().top; 77 } 78 79 public int indexOfPoint(int x, int y) { 80 Insets insets = renderTarget.getInsets(); 81 int contentWidth = renderTarget.getWidth() 82 - (insets.left + insets.right); 83 int contentHeight = renderTarget.getHeight() 84 - (insets.bottom + insets.top); 85 if (y < insets.top || y > contentHeight || x < insets.left 86 || x > contentWidth) { 87 return -1; 88 } 89 int size = model.size(); 90 int diff; 91 for (int i = 0; i < size; i++) { 92 diff = x - computeX(i); 93 if ((diff >= 0) && (diff < getW(i))) { 94 return i; 95 } 96 } 97 return -1; 98 } 99 100 public int dropIndexOfPoint(int x, int y) { 101 Insets insets = renderTarget.getInsets(); 102 int contentWidth = renderTarget.getWidth() 103 - (insets.left + insets.right); 104 int contentHeight = renderTarget.getHeight() 105 - (insets.bottom + insets.top); 106 if (y < insets.top || y > contentHeight || x < insets.left 107 || x > contentWidth) { 108 return -1; 109 } 110 int size = model.size(); 112 float tabWidth = (float) contentWidth / (float) size; 113 x = x - insets.left + (int) tabWidth / 2; 115 int result = (int) (x / tabWidth); 116 return Math.min(result, model.size()); 117 } 118 119 public void setPadding(Dimension d) { 120 } 122 123 126 private void checkIndex(int index) { 127 int size = model.size(); 128 if ((index < 0) || (index >= size)) { 129 throw new IllegalArgumentException ("Index out of valid scope 0.." 130 + (size - 1) 131 + ": " 132 + index); 133 } 134 } 135 136 140 private int computeX(int index) { 141 Insets insets = renderTarget.getInsets(); 142 int contentWidth = renderTarget.getWidth() 143 - (insets.left + insets.right); 144 return (contentWidth * index / model.size()) + insets.left; 145 } 146 147 148 } 149 | Popular Tags |