2011-10-25 9 views
8

मैं तालिका प्रस्तुत करने के लिए SWT (और ग्रहण आरसीपी) का उपयोग कर रहा हूं। मेरी समस्या यह है कि अगर मैं एक सेल की पृष्ठभूमि बदलता हूं (वास्तव में दर्शक दर्शक) मैं देख सकता हूं कि इसमें नया रंग है।चयन और होवर एक एसडब्ल्यूटी तालिका घटक में सेल पृष्ठभूमि रंग ओवरराइड करता है

मेरे समस्या यह है कि मैं सवाल में मेरे सेल वाली पंक्ति पर ले जाएं, अगर मैं अपने टेबल में या अगर एक पंक्ति का चयन तो चयन/मंडराना पृष्ठभूमि मेरे सेल रंग का अधिरोहण है। मैं इसे कैसे ओवरराइड कर सकता हूं?

उत्तर

3

StyledCellLabelProvider के साथ हल समस्या। मुझे बताओ कि क्या आप कुछ कोड देखना चाहते हैं। संपादित करें: हम यह कर प्रदर्शन सत्यापन त्रुटियों तो सत्यापन सामान यहाँ उपेक्षा का उपयोग करें:

public class ValidationCellLabelProvider extends StyledCellLabelProvider { 

    private static final int DELAY = 200; 
    private static final int SHIFT_X = 5; 
    private static final int SHIFT_Y = 5; 
    private static final int DISPLAY = 5000; 
    private CellLabelProvider provider; 
    private String propertyName; 
    private final StyleRange[] styleRanges = new StyleRange[1]; 

    /** 
    * Default constructor. 
    * @param provider provider 
    * @param propertyName propertyName 
    */ 
    public ValidationCellLabelProvider(CellLabelProvider provider, String propertyName) { 
     super(StyledCellLabelProvider.COLORS_ON_SELECTION); 
     this.provider = provider; 
     this.propertyName = propertyName; 
     this.setOwnerDrawEnabled(true); 
    } 

    @Override 
    public void initialize(ColumnViewer viewer, ViewerColumn column) { 
     super.initialize(viewer, column); 
     final StyleRange styleRange = new StyleRange(); 
     styleRange.foreground = Display.getCurrent().getSystemColor(SWT.COLOR_WHITE); 
     styleRange.background = Display.getCurrent().getSystemColor(SWT.COLOR_RED); 
     styleRanges[0] = styleRange; 
    } 

    @Override 
    public void update(ViewerCell cell) { 
     provider.update(cell); 
     if (cell.getStyleRanges() == null) { 
      cell.setStyleRanges(styleRanges); 
     } 
     if (cell.getElement() instanceof IValidable) { 
      IValidable model = (IValidable) cell.getElement(); 
      if (!ControllerRegistry.getCurrentViolations().getViolations(model.getModelId(), propertyName).isEmpty()) { 
       if (cell.getText().isEmpty()) { 
        cell.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED)); 
        cell.setImage(FieldDecorationRegistry.getDefault().getFieldDecoration(FieldDecorationRegistry.DEC_ERROR).getImage()); 
       } else { 
        if (styleRanges[0].length < cell.getText().length()) { 
         styleRanges[0].length = cell.getText().length(); 
        } 
       } 
      } else { 
       if (cell.getImage() != null) { 
        cell.setImage(null); 
       } 
       cell.setStyleRanges(null); 
      } 
     } 
     super.update(cell); 
    } 

    //mine 
    @Override 
    protected void paint(Event event, Object element) { 
     if (element instanceof IValidable) { 
      IValidable model = (IValidable) element; 
      if (!ControllerRegistry.getCurrentViolations().getViolations(model.getModelId(), propertyName).isEmpty()) { 
       int width = 1000; 
       int x = event.x; 
       int y = event.y; 

       int height = event.height - 1; 
       GC gc = event.gc; 

       Color oldBackground = gc.getBackground(); 

       gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_DARK_RED)); 

       gc.fillRectangle(x, y, width, height); 

       gc.setBackground(oldBackground); 
      } 
     } 
     super.paint(event, element); 
    } 

    //----- 

    @Override 
    public String getToolTipText(Object element) { 
     String ret = null; 
     if (element instanceof IValidable) { 
      List<ConstraintViolation> constraintViolations = ControllerRegistry.getCurrentViolations().getViolations(
        ((IValidable) element).getModelId(), propertyName); 
      if (!constraintViolations.isEmpty()) { 
       ret = ValidationHelper.getMessage(constraintViolations); 
      } 
     } 
     if (ret != null) { 
      ret = ret.length() > 0 ? ret.toString() : null; 
     } 
     return ret; 
    } 

    @Override 
    public int getToolTipDisplayDelayTime(Object object) { 
     return DELAY; 
    } 

    @Override 
    public Point getToolTipShift(Object object) { 
     return new Point(SHIFT_X, SHIFT_Y); 
    } 

    @Override 
    public int getToolTipTimeDisplayed(Object object) { 
     return DISPLAY; 
    } 

} 
0

एकमात्र विकल्प जो मैं देखता हूं वह OwnerDrawLabelProvider का उपयोग करेगा और पूरे सेल को स्वयं पेंट करेगा।

तालिका को अपनी चयन पृष्ठभूमि खींचने से रोकने का एक तरीका है लेकिन फ़ॉन्ट रंग अभी भी अपने चयन रंग में बदल जाएगा, इसलिए आपके ओएस के आधार पर आप पंक्ति के चयन के दौरान सफेद पृष्ठभूमि पर सफेद पाठ के साथ समाप्त हो सकते हैं।

+0

ऐसा लगता है कि मैं StyledCellLabelProvider के साथ समस्या को हल किया। यदि मैं इसके साथ खत्म करता हूं तो मैं समाधान प्रदान करूंगा। –

संबंधित मुद्दे