PageRenderTime 2363ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/azureus-4.7.0.2/com/aelitis/azureus/ui/swt/skin/SWTSkinObjectText1.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 378 lines | 267 code | 64 blank | 47 comment | 78 complexity | 13a7ef7dc9bea31110fd5f669e200bf7 MD5 | raw file
  1. /*
  2. * Created on Jun 26, 2006 12:46:42 PM
  3. * Copyright (C) 2006 Aelitis, All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. *
  17. * AELITIS, SAS au capital de 46,603.30 euros
  18. * 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
  19. */
  20. package com.aelitis.azureus.ui.swt.skin;
  21. import java.text.NumberFormat;
  22. import java.text.ParseException;
  23. import java.util.Locale;
  24. import org.eclipse.swt.SWT;
  25. import org.eclipse.swt.events.*;
  26. import org.eclipse.swt.graphics.*;
  27. import org.eclipse.swt.widgets.Composite;
  28. import org.eclipse.swt.widgets.Control;
  29. import org.eclipse.swt.widgets.Label;
  30. import org.gudy.azureus2.core3.internat.MessageText;
  31. import org.gudy.azureus2.core3.util.AERunnable;
  32. import org.gudy.azureus2.core3.util.Constants;
  33. import org.gudy.azureus2.ui.swt.Messages;
  34. import org.gudy.azureus2.ui.swt.Utils;
  35. import com.aelitis.azureus.ui.swt.utils.FontUtils;
  36. /**
  37. * Text Skin Object. This one uses a label widget.
  38. *
  39. * @author TuxPaper
  40. * @created Jun 26, 2006
  41. *
  42. */
  43. public class SWTSkinObjectText1
  44. extends SWTSkinObjectBasic
  45. implements SWTSkinObjectText
  46. {
  47. String sText;
  48. String sKey;
  49. boolean bIsTextDefault = false;
  50. Label label;
  51. private int style;
  52. public SWTSkinObjectText1(SWTSkin skin, SWTSkinProperties skinProperties,
  53. String sID, String sConfigID, String[] typeParams, SWTSkinObject parent) {
  54. super(skin, skinProperties, sID, sConfigID, "text", parent);
  55. style = SWT.WRAP;
  56. String sAlign = skinProperties.getStringValue(sConfigID + ".align");
  57. if (sAlign != null) {
  58. int align = SWTSkinUtils.getAlignment(sAlign, SWT.NONE);
  59. if (align != SWT.NONE) {
  60. style |= align;
  61. }
  62. }
  63. if (skinProperties.getIntValue(sConfigID + ".border", 0) == 1) {
  64. style |= SWT.BORDER;
  65. }
  66. Composite createOn;
  67. if (parent == null) {
  68. createOn = skin.getShell();
  69. } else {
  70. createOn = (Composite) parent.getControl();
  71. }
  72. boolean bKeepMaxSize = properties.getStringValue(
  73. sConfigID + ".keepMaxSize", "").equals("1");
  74. label = bKeepMaxSize ? new LabelNoShrink(createOn, style) : new Label(
  75. createOn, style);
  76. setControl(label);
  77. if (typeParams.length > 1) {
  78. bIsTextDefault = true;
  79. sText = typeParams[1];
  80. label.setText(sText);
  81. }
  82. }
  83. public String switchSuffix(String suffix, int level, boolean walkUp, boolean walkDown) {
  84. suffix = super.switchSuffix(suffix, level, walkUp, walkDown);
  85. if (suffix == null) {
  86. return null;
  87. }
  88. String sPrefix = sConfigID + ".text";
  89. if (sText == null || bIsTextDefault) {
  90. String text = properties.getStringValue(sPrefix + suffix);
  91. if (text != null) {
  92. label.setText(text);
  93. }
  94. }
  95. Color color = properties.getColor(sPrefix + ".color" + suffix);
  96. //System.out.println(this + "; " + sPrefix + ";" + suffix + "; " + color + "; " + text);
  97. if (color != null) {
  98. label.setForeground(color);
  99. }
  100. Font existingFont = (Font) label.getData("Font" + suffix);
  101. if (existingFont != null && !existingFont.isDisposed()) {
  102. label.setFont(existingFont);
  103. } else {
  104. boolean bNewFont = false;
  105. float fontSize = -1;
  106. int iFontWeight = -1;
  107. String sFontFace = null;
  108. String sSize = properties.getStringValue(sPrefix + ".size" + suffix);
  109. if (sSize != null) {
  110. FontData[] fd = label.getFont().getFontData();
  111. try {
  112. char firstChar = sSize.charAt(0);
  113. if (firstChar == '+' || firstChar == '-') {
  114. sSize = sSize.substring(1);
  115. }
  116. int iSize = NumberFormat.getInstance(Locale.US).parse(sSize).intValue();
  117. if (firstChar == '+') {
  118. fontSize = (int) (fd[0].height + iSize);
  119. } else if (firstChar == '-') {
  120. fontSize = (int) (fd[0].height - iSize);
  121. } else {
  122. fontSize = iSize;
  123. }
  124. if (sSize.endsWith("px")) {
  125. fontSize = FontUtils.getFontHeightFromPX(label.getFont(), null, iSize);
  126. }
  127. bNewFont = true;
  128. } catch (NumberFormatException e) {
  129. e.printStackTrace();
  130. } catch (ParseException e) {
  131. // TODO Auto-generated catch block
  132. e.printStackTrace();
  133. }
  134. }
  135. String sStyle = properties.getStringValue(sPrefix + ".style" + suffix);
  136. if (sStyle != null) {
  137. String[] sStyles = Constants.PAT_SPLIT_COMMA.split(sStyle.toLowerCase());
  138. for (int i = 0; i < sStyles.length; i++) {
  139. String s = sStyles[i];
  140. if (s.equals("bold")) {
  141. if (iFontWeight == -1) {
  142. iFontWeight = SWT.BOLD;
  143. } else {
  144. iFontWeight |= SWT.BOLD;
  145. }
  146. bNewFont = true;
  147. }
  148. if (s.equals("italic")) {
  149. if (iFontWeight == -1) {
  150. iFontWeight = SWT.ITALIC;
  151. } else {
  152. iFontWeight |= SWT.ITALIC;
  153. }
  154. bNewFont = true;
  155. }
  156. if (s.equals("underline")) {
  157. label.addPaintListener(new PaintListener() {
  158. public void paintControl(PaintEvent e) {
  159. Point size = ((Control) e.widget).getSize();
  160. e.gc.drawLine(0, size.y - 1, size.x - 1, size.y - 1);
  161. }
  162. });
  163. }
  164. if (s.equals("strike")) {
  165. label.addPaintListener(new PaintListener() {
  166. public void paintControl(PaintEvent e) {
  167. Point size = ((Control) e.widget).getSize();
  168. int y = size.y / 2;
  169. e.gc.drawLine(0, y, size.x - 1, y);
  170. }
  171. });
  172. }
  173. }
  174. }
  175. sFontFace = properties.getStringValue(sPrefix + ".font" + suffix);
  176. if (sFontFace != null) {
  177. bNewFont = true;
  178. }
  179. if (bNewFont) {
  180. FontData[] fd = label.getFont().getFontData();
  181. if (fontSize > 0) {
  182. FontUtils.setFontDataHeight(fd, fontSize);
  183. }
  184. if (iFontWeight >= 0) {
  185. fd[0].setStyle(iFontWeight);
  186. }
  187. if (sFontFace != null) {
  188. fd[0].setName(sFontFace);
  189. }
  190. final Font labelFont = new Font(label.getDisplay(), fd);
  191. label.setFont(labelFont);
  192. label.addDisposeListener(new DisposeListener() {
  193. public void widgetDisposed(DisposeEvent e) {
  194. labelFont.dispose();
  195. }
  196. });
  197. label.setData("Font" + suffix, labelFont);
  198. }
  199. }
  200. label.update();
  201. return suffix;
  202. }
  203. /**
  204. * @param searchText
  205. */
  206. public void setText(String text) {
  207. if (text == null) {
  208. text = "";
  209. }
  210. if (text.equals(sText)) {
  211. return;
  212. }
  213. this.sText = text;
  214. this.sKey = null;
  215. bIsTextDefault = false;
  216. Utils.execSWTThread(new AERunnable() {
  217. public void runSupport() {
  218. if (label != null && !label.isDisposed()) {
  219. label.setText(sText);
  220. Utils.relayout(label);
  221. }
  222. }
  223. });
  224. }
  225. public void setTextID(final String key) {
  226. if (key == null) {
  227. setText("");
  228. }
  229. else if (key.equals(sKey)) {
  230. return;
  231. }
  232. this.sText = MessageText.getString(key);
  233. this.sKey = key;
  234. bIsTextDefault = false;
  235. Utils.execSWTThread(new AERunnable() {
  236. public void runSupport() {
  237. if (label != null && !label.isDisposed()) {
  238. Messages.setLanguageText(label, key);
  239. Utils.relayout(label);
  240. }
  241. }
  242. });
  243. }
  244. public void setTextID(final String key, final String[] params) {
  245. if (key == null) {
  246. setText("");
  247. } else if (key.equals(sKey)) {
  248. return;
  249. }
  250. this.sText = MessageText.getString(key);
  251. this.sKey = key;
  252. bIsTextDefault = false;
  253. Utils.execSWTThread(new AERunnable() {
  254. public void runSupport() {
  255. if (label != null && !label.isDisposed()) {
  256. Messages.setLanguageText(label, key, params);
  257. Utils.relayout(label);
  258. }
  259. }
  260. });
  261. }
  262. private class LabelNoShrink
  263. extends Label
  264. {
  265. Point ptMax;
  266. /**
  267. * Default Constructor
  268. *
  269. * @param parent
  270. * @param style
  271. */
  272. public LabelNoShrink(Composite parent, int style) {
  273. super(parent, style | SWT.CENTER);
  274. ptMax = new Point(0, 0);
  275. }
  276. // I know what I'm doing. Maybe ;)
  277. public void checkSubclass() {
  278. }
  279. public Point computeSize(int wHint, int hHint, boolean changed) {
  280. Point pt = super.computeSize(wHint, hHint, changed);
  281. if (pt.x > ptMax.x) {
  282. ptMax.x = pt.x;
  283. }
  284. if (pt.y > ptMax.y) {
  285. ptMax.y = pt.y;
  286. }
  287. return ptMax;
  288. }
  289. }
  290. // @see com.aelitis.azureus.ui.swt.skin.SWTSkinObjectText#getStyle()
  291. public int getStyle() {
  292. return style;
  293. }
  294. // @see com.aelitis.azureus.ui.swt.skin.SWTSkinObjectText#setStyle(int)
  295. public void setStyle(int style) {
  296. this.style = style;
  297. }
  298. // @see com.aelitis.azureus.ui.swt.skin.SWTSkinObjectText#getText()
  299. public String getText() {
  300. return sText;
  301. }
  302. // @see com.aelitis.azureus.ui.swt.skin.SWTSkinObjectText#addUrlClickedListener(com.aelitis.azureus.ui.swt.skin.SWTSkinObjectText_UrlClickedListener)
  303. public void addUrlClickedListener(SWTSkinObjectText_UrlClickedListener l) {
  304. // TODO Auto-generated method stub
  305. }
  306. // @see com.aelitis.azureus.ui.swt.skin.SWTSkinObjectText#removeUrlClickedListener(com.aelitis.azureus.ui.swt.skin.SWTSkinObjectText_UrlClickedListener)
  307. public void removeUrlClickedListener(SWTSkinObjectText_UrlClickedListener l) {
  308. // TODO Auto-generated method stub
  309. }
  310. // @see com.aelitis.azureus.ui.swt.skin.SWTSkinObjectText#setTextColor(org.eclipse.swt.graphics.Color)
  311. public void setTextColor(Color color) {
  312. // TODO Auto-generated method stub
  313. }
  314. }