简体   繁体   English

使用SWT更改文本时标签被切断

[英]Label cut off when changing text using SWT

So I build a shell that looks like the following 因此,我构建了一个如下所示的外壳 在此处输入图片说明

After I click the change path button, I prompt for the path and then set the text. 单击更改路径按钮后,提示输入路径,然后设置文本。 The problem is after doing so new text gets cut off if its too long like so. 问题是这样做之后,如果新文本太长,就会被截断。 在此处输入图片说明

I am using a Label to display the path, and the only method I'm using in the listener regarding the Laebl is the setText() method. 我正在使用Label来显示路径,并且在侦听器中有关Laebl的唯一方法是setText()方法。 Is there a way to not have this happen? 有没有办法避免这种情况发生? I am using SWT and prefer to maintain the grid layout so I can have the 2 columns. 我正在使用SWT,并且更喜欢维护网格布局,因此我可以使用2列。 Any info would be helpful. 任何信息都会有帮助。 Thank you. 谢谢。

Here's the code 这是代码

String unit[] = {"Pixil", "Inch"};

                    final CustomAttribute realWidth = new CustomAttribute(String.valueOf(obj.getGraph().getWidth(null)));
                    final CustomAttribute realHeight = new CustomAttribute(String.valueOf(obj.getGraph().getHeight(null)));

                    double[] sizes = obj.convertToSizes();
                    if(sizes != null){
                        realWidth.setValue(sizes[0]);
                        realHeight.setValue(sizes[1]);
                    }

                    final Shell shell = new Shell(d);
                    shell.setImage(ApplicationPlugin.getImage(ApplicationPlugin.QUATTRO_ICON));
                    shell.setLayout(new org.eclipse.swt.layout.GridLayout(2,true));
                    shell.setText(EDIT_IMG_WINDOW_TITLE);

                    //width info
                    final Label labelWidth = new Label(shell, SWT.HORIZONTAL);
                    final Text textWidth = new Text(shell,SWT.SINGLE | SWT.BORDER);

                    //height info
                    final Label labelHeight = new Label(shell, SWT.HORIZONTAL);
                    final Text textHeight = new Text(shell,SWT.SINGLE | SWT.BORDER);

                    //units info
                    final Combo unitCombo = new Combo (shell, SWT.READ_ONLY);
                    final Button ratioBox = new Button (shell, SWT.CHECK);
                    ratioBox.setText(EDIT_IMG_RADIO);

                    //path info
                    final Button pathButton = new Button (shell, SWT.PUSH);
                    pathButton.setText(EDIT_IMG_PATH);
                    final Label pathText = new Label(shell, SWT.HORIZONTAL);
                    pathText.setText(obj.getTextData()[0]);

                    Button change = new Button (shell, SWT.PUSH);
                    change.setText(EDIT_IMG_SAVE_BUTTON);
                    ModifyListener heightListener = new ModifyListener(){
                        public void modifyText(ModifyEvent e) {
                            if(realImgListen && textHeight.getText() != ""){
                                realImgListen = false;
                                try{
                                    double oldHeight = realHeight.getDouble();
                                    double newHeight = Double.parseDouble(textHeight.getText());
                                    double oldWidth = Double.parseDouble(textWidth.getText());
                                    if(unitCombo.getSelectionIndex() == 1){
                                        newHeight = newHeight * designer.getPixilPerInch();
                                        oldWidth = oldWidth * designer.getPixilPerInch();
                                    }   
                                    realHeight.setValue(newHeight);
                                    double[] sizes = obj.convertToSizes();
                                    if(sizes != null)
                                        realWidth.setValue(sizes[0]);
                                    else
                                        realWidth.setValue(oldWidth);

                                    if(realHeight.getDouble() > SheetCanvas.sheetYSize)
                                        realHeight.setValue(SheetCanvas.sheetYSize);
                                    if(realHeight.getDouble() < 1)
                                        realHeight.setValue(1);

                                    if(ratioBox.getSelection() == true){
                                        double scale = Double.parseDouble(realHeight.getValue()) / oldHeight;
                                        realWidth.setValue(String.valueOf(realWidth.getDouble()*scale));
                                        if(unitCombo.getSelectionIndex() == 0)
                                            textWidth.setText(String.valueOf((int)Math.round(Double.parseDouble(realWidth.getValue()))));
                                        else
                                            textWidth.setText(String.valueOf(Double.parseDouble(realWidth.getValue())/designer.getPixilPerInch()));
                                    }
                                    obj.storeSizingInfo(realWidth.getDouble(), realHeight.getDouble(), 0);
                                    realImgListen = true;
                                }
                                catch(NumberFormatException e2){
                                    realImgListen = true;
                                }
                            }
                        }
                    };
                    ModifyListener widthListener = new ModifyListener(){
                        public void modifyText(ModifyEvent e) {
                            if(realImgListen && textHeight.getText() != ""){
                                realImgListen = false;
                                try{
                                    double oldWidth = realWidth.getDouble();
                                    double newWidth = Double.parseDouble(textWidth.getText());
                                    double oldHeight = Double.parseDouble(textHeight.getText());
                                    if(unitCombo.getSelectionIndex() == 1){
                                        newWidth = newWidth * designer.getPixilPerInch();
                                        oldHeight = oldHeight * designer.getPixilPerInch();
                                    }   
                                    realWidth.setValue(newWidth);
                                    double[] sizes = obj.convertToSizes();
                                    if(sizes != null)
                                        realHeight.setValue(sizes[1]);
                                    else
                                        realHeight.setValue(oldHeight);

                                    if(realWidth.getDouble() > SheetCanvas.sheetYSize)
                                        realWidth.setValue(SheetCanvas.sheetYSize);
                                    if(realWidth.getDouble() < 1)
                                        realWidth.setValue(1);

                                    if(ratioBox.getSelection() == true){
                                        double scale = Double.parseDouble(realWidth.getValue()) / oldWidth;
                                        realHeight.setValue(String.valueOf(realHeight.getDouble()*scale));
                                        if(unitCombo.getSelectionIndex() == 0)
                                            textHeight.setText(String.valueOf((int)Math.round(Double.parseDouble(realHeight.getValue()))));
                                        else
                                            textHeight.setText(String.valueOf(Double.parseDouble(realHeight.getValue())/designer.getPixilPerInch()));
                                    }
                                    obj.storeSizingInfo(realWidth.getDouble(), realHeight.getDouble(), 0);
                                    realImgListen = true;
                                }
                                catch(NumberFormatException e2){
                                    realImgListen = true;
                                }
                            }
                        }
                    };
                    textHeight.addModifyListener(heightListener);
                    textWidth.addModifyListener(widthListener);
                    unitCombo.addSelectionListener(new SelectionAdapter() {
                        @Override
                        public void widgetSelected(SelectionEvent e) {
                            realImgListen = false;
                            if(unitCombo.getSelectionIndex() == 0){
                                textWidth.setText(String.valueOf((int)Math.rint(realWidth.getDouble())));
                                textHeight.setText(String.valueOf((int)Math.rint(realHeight.getDouble())));
                            }
                            else{
                                textWidth.setText(String.valueOf(realWidth.getDouble()/designer.getPixilPerInch()));
                                textHeight.setText(String.valueOf(realHeight.getDouble()/designer.getPixilPerInch()));
                            }
                            realImgListen = true;
                        }
                    });
                    change.addSelectionListener(new SelectionAdapter() {
                        @Override
                        public void widgetSelected(SelectionEvent e) {

                            Double width = realWidth.getDouble();
                            Double height = realHeight.getDouble();

                            String line1;
                            if(obj.getTextData() != null)
                                line1 = obj.getTextData()[0];
                            else
                                line1 = null;
                            String[] textData = {line1,null};
                            obj.setTextData(textData);
                            obj.storeSizingInfo(Math.rint(width), Math.rint(height),0);
                            designer.updateDataFromSource(settings);
                            designer.repaint();
                            updatePanelButtons();
                            shell.close();
                        }
                    });
                    pathButton.addSelectionListener(new SelectionAdapter() {
                        @Override
                        public void widgetSelected(SelectionEvent e) {
                            //path button
                            String path = null;
                            path = getPathFromBrowser(FILE_CHOOSER_IMAGE_TITLE_STR,DEFAULT_PATH,acceptedImgFormats,null,SWT.OPEN);
                            pathText.setText(path);
                            if(path != null){
                                String[] paths = settings.getCustomImagePath();
                                int pathIndex = 0;
                                for(int i = 0; i < paths.length; i++){
                                    if(obj.getTextData()[0].equals(paths[i]))
                                        pathIndex = i;
                                }
                                paths[pathIndex] = path;
                                settings.setCustomImagePath(paths);
                                paths = settings.getCustomImageChoices();
                                paths[pathIndex] = getFileNameFromPath(path);
                                settings.setCustomImageChoices(paths);
                                designer.updateDataFromSource(settings);
                                String[] textData = obj.getTextData();
                                textData[0] = path;
                                if(textData.length > 1)
                                    textData[1] = null;
                                obj.setTextData(textData);
                            }
                        }
                    });

                    textWidth.setTextLimit(7);
                    textHeight.setTextLimit(7);
                    labelWidth.setText(EDIT_IMG_WIDTH);
                    labelHeight.setText(EDIT_IMG_HEIGHT);
                    unitCombo.setItems(unit);
                    unitCombo.select(0);
                    ratioBox.setSelection(true);
                    org.eclipse.swt.graphics.Rectangle clientArea = shell.getClientArea();
                    unitCombo.setBounds(clientArea.x, clientArea.y, 300, 200);
                    shell.pack();
                    shell.open();
                    Shell shellTemp = SWT_AWT.new_Shell(d, designer);
                    Monitor primary = d.getPrimaryMonitor();
                    org.eclipse.swt.graphics.Rectangle bounds = primary.getBounds();
                    org.eclipse.swt.graphics.Rectangle rect = shell.getBounds();
                    int x = bounds.x + (bounds.width - rect.width) / 2;
                    int y = bounds.y + (bounds.height - rect.height) / 2;
                    shell.setLocation(x, y);
                    shell.moveAbove(shellTemp);
                    shellTemp.dispose();
                    shell.addListener(SWT.Close, new Listener() {
                        @Override  
                        public void handleEvent(Event event) {
                            editingImg();
                          }
                        });
                    realImgListen = false;
                    textWidth.setText(String.valueOf((int)Double.parseDouble(realWidth.getValue())));
                    textHeight.setText(String.valueOf((int)Double.parseDouble(realHeight.getValue())));
                    realImgListen = true;

The width of your columns is currently being set to the width of the widest control - probably 'Maintain Image Ratio'. 列的宽度当前设置为最宽控件的宽度-可能是“保持图像比率”。 If you set the the path text to anything longer than that it will be truncated. 如果将路径文本设置为比该长度更长的内容,它将被截断。

You could set a width hint on the path control to set its size: 您可以在路径控件上设置宽度提示以设置其大小:

GridData data = new GridData(SWT.FILL, SWT.CENTER, true, false);
data.widthHint = 100;  // You choose the width
pathText.setLayoutData(data);

Note that you have specified that the columns are of equal width so this will add similar space to the first column. 请注意,您已指定列的宽度相等,因此这将为第一列添加类似的空间。 You might want to switch to unequal width columns. 您可能要切换到不等宽的列。

Alternatively you can ask the Shell to recalculate the size after you set the new text. 或者,您可以在设置新文本后要求Shell重新计算大小。 Just call: 只需致电:

 shell.pack();

It looks like that you are redrawing the change path label components and you are not wrapping the label . 看起来您正在重新绘制更改路径标签组件,并且没有包装标签。 That why at the time of redrawing some part of the text is not visible. 这就是为什么在重画部分文字时看不到的原因。

Use warping or you can also increase the screen size 使用变形,也可以增加屏幕尺寸

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM